单击div中的任何内容时的jQuery事件单击 [英] jQuery event click when clicked on any content inside a div
本文介绍了单击div中的任何内容时的jQuery事件单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个jQuery:
I have this jQuery:
$("div.result").live("click", function(event){
alert("kokoko");
});
HTML可能是:
<div id="result">
Flash banner code
Image with link
etc.
</div>
我想做的是当单击div支架内的任何元素时触发警报.当前,当结果中出现图像横幅时,不会触发警报.
What I want to do is to trigger the alert when any of the elements inside the div holder is clicked on. Currently the alert is not trigger when there is a image banner inside result.
推荐答案
尝试:
$("#result").on("click", function(){
alert("kokoko");
});
// or
$("#result").click(function(){
alert("kokoko");
});
// or just pure JavaScript
document.getElementById("result").addEventListener("click",function(){
alert("kokoko");
});
尽管使用这样的纯JavaScript(具有addEvent
和removeEvent
函数)解决跨浏览器的问题是一个好习惯:
Though it's a good practice to solve cross-browser issues with pure JavaScript like this (with addEvent
and removeEvent
functions):
(function(){
if ( document.addEventListener ) {
this.addEvent = function(elem, type, fn) {
elem.addEventListener(type, fn, false);
return fn;
};
this.removeEvent = function(elem, type, fn) {
elem.removeEventListener(type, fn, false);
};
} else if ( document.attachEvent ) {
this.addEvent = function(elem, type, fn) {
var bound = function() {
return fn.apply(elem, arguments);
};
elem.attachEvent("on" + type, bound);
return bound;
};
this.removeEvent = function (elem, type, fn) {
elem.detachEvent("on" + type, fn);
};
}
})();
这篇关于单击div中的任何内容时的jQuery事件单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文