javascript jQuery 单击绑定不会在 IE8 中触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13961990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Click binding does not fire in IE8
提问by contactmatt
The jQuery click binding is never fired in IE8 (works in IE9, Chrome, etc.) How can I get the click event to fire in IE8, and why is it not currently?
在 IE8 中永远不会触发 jQuery 单击绑定(在 IE9、Chrome 等中工作)如何让单击事件在 IE8 中触发,为什么当前没有?
Note: I've tried changing the href to '#', but that is not working either.
注意:我尝试将 href 更改为“#”,但这也不起作用。
$('<a/>', {
id: 'anchor-id',
href: 'javascript:void(0);',
'class': 'button-next',
html: 'HTML'
}).click(function() {
alert('clicked');
}).appendTo($('#append'));?
version: 1.7.2
版本:1.7.2
回答by Jason Towne
Have you tried using .on()
?
你试过使用.on()
吗?
.on()
will create an event handler for all current and future elements that match your selector. In the statement below the click
event will be handled for all elements with a class of button-next
regardless of when they are added to the DOM.
.on()
将为与您的选择器匹配的所有当前和未来元素创建一个事件处理程序。在下面的语句中,click
将针对所有具有类的元素处理事件,button-next
而不管它们何时添加到 DOM。
$(document).on('click', '.button-next', function(e)
{
//do something
});
回答by Huangism
Try something like this
尝试这样的事情
$(document).on('click', '.button-next', function() {
// code here
});
回答by CodeLikeBeaker
I was able to get it to work in IE8 using the following:
我能够使用以下方法让它在 IE8 中工作:
<div id="append"></div>
<script>
var aLink = $('<a/>', { id: 'anchor-id', href: '#',html : 'click me'}).on('click', function () {
alert('clicked');
});
$('#append').append(aLink);
</script>
Also, make sure you do not have any console.log()'s in there. IE does not like them very much.
另外,请确保那里没有任何 console.log()。IE 不太喜欢它们。
回答by oomlaut
Have you stopped event propagation?
您是否停止了事件传播?
Typically with links you have to do something like:
通常,对于链接,您必须执行以下操作:
<a href="#">HTML</a>
This script would work:
这个脚本可以工作:
jQuery('#someLink').bind('click', function(e){
e.preventDefault();
//.. do something
});
NOTE:your javascript:void(); SHOULDhave covered this, but just checking.
注意:你的 javascript:void(); 应该已经涵盖了这一点,但只是检查。
If it's not a "real" link, you're not forced to use the A
element, as you can simulate hover events with the same jQuery that creates the object:
如果它不是“真实”链接,则不会强制您使用该A
元素,因为您可以使用创建对象的相同 jQuery 模拟悬停事件:
jQuery("<span>", {
text: "html",
id: "anchor-id",
"class": "button-next",
click: function() { /* do something */ },
mouseover: function() { /* hover over */ },
mouseout: function() { /* hover out */ }
}).appendTo(jQuery('#append'));