JavaScript:使用或不使用jQuery来改变onclick的价值

我想更改锚点上onclick属性的值。 我想将其设置为包含JavaScript的新string。 (这个string是由服务器提供给客户端的JavaScript代码的,它可以包含任何你可以放在HTML中的onclick属性中。)下面是我尝试的一些东西:

  • 使用jQuery attr("onclick", js)不适用于Firefox和IE6 / 7。
  • 使用setAttribute("onclick", js)可以与Firefox和IE8一起使用,但不能使用IE6 / 7。
  • 使用onclick = function() { return eval(js); } onclick = function() { return eval(js); }不起作用,因为您不允许使用return代码传递给eval()

任何人都有build议将onclick属性设置为使Firefox和IE 6/7/8的工作? 另请参阅下面的代码,我用来testing这个。

 <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> 

如果您使用jQuery,则不应再使用onClick 。 jQuery提供了自己的附加和绑定事件的方法。 请参阅.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); }); 

这应该取消onClick函数 – 并保持您的“来自一个string的JavaScript”以及。

最好的办法是从HTML代码中的<a>元素中删除onclick="" ,并切换到使用Unobtrusive绑定事件的方法进行单击。

你还说:

使用onclick = function() { return eval(js); } onclick = function() { return eval(js); }不起作用,因为您不允许在传递给eval()的代码中使用返回值。

不 – 它不会,但是onclick = eval("(function(){"+js+"})"); 将“js”variables包装在function框中。 onclick = new Function(js); 也可以工作,阅读起来更清洁一些。 (注意大写字母F) – 请参阅Function()构造Function()文档

顺便说一句,没有JQuery这也可以做到,但显然这是非常丑陋的,因为它只考虑IE /非IE:

 if(isie) tmpobject.setAttribute('onclick',(new Function(tmp.nextSibling.getAttributeNode('onclick').value))); else $(tmpobject).attr('onclick',tmp.nextSibling.attributes[0].value); //this even supposes index 

无论如何,只是为了让人们对可以做什么有一个全面的了解,因为我相信很多人都已经绊倒了这个烦恼。

JQuery的一个问题是,点击function不承认从HTML手动编码onclick。

所以,你几乎不得不select。 在init函数中设置所有的处理程序,或者在html中设置所有的处理程序。

JQuery中的click事件是点击函数$(“myelt”)。click(function ….)。

只是使用jQuery绑定方法!jquery-selector!.bind('event',!fn!);

有关jQuery中的事件的更多信息,请参阅此处

如果你不想实际导航到一个新的页面,你也可以像这样在页面上的某处放置锚点。

 <a id="the_anchor" href=""> 

然后把你的JavaScriptstring分配给锚点的onclick,把这个放在别的地方(比如头,稍后放在body里面,不pipe):

 <script> var js = "alert('I am your string of JavaScript');"; // js is your string of script document.getElementById('the_anchor').href = 'javascript:' + js; </script> 

如果在发送页面之前将所有这些信息都放在服务器上,那么也可以直接将JavaScript直接放置在锚点的href属性中,如下所示:

 <a href="javascript:alert('I am your script.'); alert('So am I.');">Click me</a> 

请注意,按照gnarf的想法,你也可以这样做:

 var js = "alert('B:' + this.id); return false;";<br/> var newclick = eval("(function(){"+js+"});");<br/> $("a").get(0).onclick = newclick; 

这将设置onclick而不触发事件(有同样的问题在这里,我花了一些时间来找出)。

想出了一个快速和肮脏的修复这个。 刚刚使用<select onchange='this.options[this.selectedIndex].onclick();> <option onclick='alert("hello world")' ></option> </select>

希望这可以帮助