jQuery设置onclick属性只在FireFox上工作? [英] jQuery setting onclick attribute working only on FireFox?

查看:167
本文介绍了jQuery设置onclick属性只在FireFox上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



虽然其他属性可以正常工作(onclick )例如.href,还有其他自定义attrs),设置 onclick 似乎只能在 FireFox 中使用。在其他浏览器上,该属性被忽略。



任何提示?这种行为是正确的吗?为什么?

 < html>< head> 
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js>< / script>
< script type =text / javascript>
jQuery(function(){
var obj = $('< a />');
obj.html('click me');
obj.attr ({'href':'#test','onclick':'alert(o hai); return false;'});
('body')。append(obj);
});
< / script>
< / head>< body />< / html>

编辑:我了解.click但这不是重点,我只是想设置属性,并不能找出为什么它不像其他attr那样工作。

解决方案
$ b $ p $

这个行为正确吗?

> attr('onsomeevent',...)从来就不是添加事件监听器的方式,从来没有被支持,只能在Firefox上工作,因为巧合。
$ b


为什么?

直接获取/设置HTML属性。调用 setAttribute()只设置一个同名的DOM属性。而 onclick 属性不是字符串属性,所以你不能写一个字符串给它。它只接受一个函数值。

jQuery的 attr()被称为 attr 但实际上写入DOM属性而不是HTML属性...但是使用HTML属性名称(例如。 class )而不是DOM属性(例如 className )。



在某些情况下,它试图通过使用属性来隐藏它们之间的区别,除了,IE仍然不能在IE中工作,这不是jQuery最自豪的时刻之一。 >

无论如何,一般而言, JavaScript中的一个字符串是一种代码味道。一般来说,jQuery和现代JavaScript最好的方法是使用函数对象(可能是内联 function(){...} 表达式)。


I'm trying to set the "onclick" attribute of a link generated with jQuery.

While other attributes work without problem (eg. href, but also other custom attrs), setting "onclick" seems to only work in FireFox. On other browsers the attribute just gets ignored.

Any hint? Is this behaviour correct? And why?

<html><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
  jQuery(function(){
    var obj = $('<a/>');
    obj.html('click me');
    obj.attr({'href':'#test', 'onclick':'alert("o hai"); return false;'});
    $('body').append(obj);
  });
</script>
</head><body /></html>

EDIT: I know about the .click() jQuery method; but that's not the point, I just want to set the attribute and can't figure out why it doesn't work like any other attr does.

解决方案

Is this behaviour correct?

In as much as attr('onsomeevent', ...) has never been the way to add event listeners, has never been supported, and only works on Firefox due to coincidence, yes.

And why?

Because IE before version 8 can't directly get/set HTML attributes. Calling setAttribute() only sets a DOM property of the same name. And the onclick property isn't a string property so you can't write a string to it. It only accepts a function value.

jQuery's attr() muddies the issue by being called attr but actually writing to DOM properties and not HTML attributes... but using HTML attribute names (like eg. class) rather than DOM property names (like className).

(Except when it doesn't, because for some cases it tries to hide the difference by using attributes. Except that still doesn't work all the time in IE for the reason above. This is not one of jQuery's proudest moments.)

Anyhow, in general having JavaScript in a string is a code smell. The best way forward for jQuery and modern JavaScript in general is to use function objects (possibly inline function() { ... } expressions) instead.

这篇关于jQuery设置onclick属性只在FireFox上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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