I have this jQuery:
$("div.result").live("click", function(event){
alert("kokoko");
});
HTML could be:
<div id="result">
Flash banner code
Image with link
etc.
</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.
Try:
$("#result").on("click", function(){
alert("kokoko");
});
// or
$("#result").click(function(){
alert("kokoko");
});
// or just pure JavaScript
document.getElementById("result").addEventListener("click",function(){
alert("kokoko");
});
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);
};
}
})();
Never use or recommend .live() which is deprecated...
$("#result").on("click", function(event){
alert("kokoko");
});
This should do it:
$("#result").on("click", function(){
alert("it worked!");
});
live is deprecated
should be div#result .result would be a class. Also, try not to use live, use .on() instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.