JavaScript的:有或无的jQuery改变的onclick的价值 [英] JavaScript: changing the value of onclick with or without jQuery

查看:115
本文介绍了JavaScript的:有或无的jQuery改变的onclick的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想换一个锚的的onclick 属性的值。我想将其设置为包含JavaScript的一个新的字符串。 (该字符串由服务器提供给客户端的JavaScript code,它可以包含你可以把任何在HTML中的的onclick 属性。)这里有几件事情我想:


  • 使用jQuery ATTR(onclick事件,JS)不与Firefox和IE6 / 7。
  • 工作
  • 使用的setAttribute(onclick事件,JS)适用于Firefox和IE8,而不是IE6 / 7。

  • 使用的onclick =函数(){返回的eval(JS); } 不起作用,因为你不能使用收益是code传递到的eval()

任何人有设置onclick属性,以使这项工作对Firefox和IE 6/7/8有何建议?另请参见下面的code我用来测试这一点。

 < HTML和GT;
    < HEAD>
        <脚本类型=文/ JavaScript的
                SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js>&下; /脚本>
        <脚本类型=文/ JavaScript的>
            $(文件)。就绪(函数(){
                VAR JS =警报('B');返回false;;
                //设置使用jQuery:不工作
                $(A)ATTR(onclick事件,JS)。
                //设置与的setAttribute():至少在Firefox工程
                //document.getElementById(\"anchor\").setAttribute(\"onclick,JS);
            });
        < / SCRIPT>
    < /头>
    <身体GT;
        < A HREF =htt​​p://www.google.com/ID =锚的onclick =警报('A');返回false;>点击< / A>
    < /身体GT;
< / HTML>


解决方案

您不应该使用的onClick 更多,如果你正在使用jQuery。 jQuery提供了自己的连接,并结合事件的方法。请参见 。点击()

  $(文件)。就绪(函数(){
    VAR JS =警报('B:'+ this.id);返回false;;
    //从JS的字符串创建一个函数
    VAR newclick =新功能(JS);    //清除的onclick然后设置使用jQuery点击
    $(#主播)ATTR('的onclick','')。点击(newclick);
});

这应该取消的onClick 功能 - 并保持你的的JavaScript字符串,以及

做的最好的事情是删除的onclick =< A> 元素在HTML code,然后切换到使用不显眼 的事件结合的方法点击

您也说:


  

使用的onclick =函数(){返回的eval(JS); } 不起作用,因为你不能使用在传递给eval(code)的回报。


没有 - 它不会,但的onclick =的eval((函数(){+ JS +})); 将包装'JS'变量在函数外壳。 的onclick =新功能(JS); 工程,以及和有点​​清洁阅读。 (注意是大写F) - 见<一href=\"https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function\">documentation在函数()构造

I'd like to change the value of the onclick attribute on an anchor. I want to set it to a new string that contains JavaScript. (That string is provided to the client-side JavaScript code by the server, and it can contains whatever you can put in the onclick attribute in HTML.) Here are a few things I tried:

  • Using jQuery attr("onclick", js) doesn't work with both Firefox and IE6/7.
  • Using setAttribute("onclick", js) works with Firefox and IE8, but not IE6/7.
  • Using onclick = function() { return eval(js); } doesn't work because you are not allowed to use return is code passed to eval().

Anyone has a suggestion on to set the onclick attribute to to make this work for Firefox and IE 6/7/8? Also see below the code I used to test this.

<html>
    <head>
        <script type="text/javascript"
                src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                var js = "alert('B'); return false;";
                // Set with JQuery: doesn't work
                $("a").attr("onclick", js);
                // Set with setAttribute(): at least works with Firefox
                //document.getElementById("anchor").setAttribute("onclick", js);
            });
        </script>
    </head>
    <body>
        <a href="http://www.google.com/" id="anchor" onclick="alert('A'); return false;">Click</a>
    </body>
</html>

解决方案

You shouldn't be using onClick any more if you are using jQuery. jQuery provides its own methods of attaching and binding events. See .click()

$(document).ready(function(){
    var js = "alert('B:' + this.id); return false;";
    // create a function from the "js" string
    var newclick = new Function(js);

    // clears onclick then sets click using jQuery
    $("#anchor").attr('onclick', '').click(newclick);
});

That should cancel the onClick function - and keep your "javascript from a string" as well.

The best thing to do would be to remove the onclick="" from the <a> element in the HTML code and switch to using the Unobtrusive method of binding an event to click.

You also said:

Using onclick = function() { return eval(js); } doesn't work because you are not allowed to use return in code passed to eval().

No - it won't, but onclick = eval("(function(){"+js+"})"); will wrap the 'js' variable in a function enclosure. onclick = new Function(js); works as well and is a little cleaner to read. (note the capital F) -- see documentation on Function() constructors

这篇关于JavaScript的:有或无的jQuery改变的onclick的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆