I have a onclick function like this
onclick = function(){alert('hello')}
I want to add an additional function to it base on the server response so that I can display more, like not only display hello but also 'you are wele', how to writ this without jquery?
onclick = function(){alert('you are wele')}
thank you for helping.
I have a onclick function like this
onclick = function(){alert('hello')}
I want to add an additional function to it base on the server response so that I can display more, like not only display hello but also 'you are wele', how to writ this without jquery?
onclick = function(){alert('you are wele')}
thank you for helping.
Share Improve this question asked Jun 27, 2012 at 3:59 pandapanda 1,3443 gold badges14 silver badges35 bronze badges4 Answers
Reset to default 5Use addEventListener
.
elementSelected.addEventListener("click", function(){
alert('you are wele');
});
This is a modern way to do that.
For that old IE, you have to do this instead:
elementSelected.attachEvent("onclick", function(){
alert('you are wele');
});
So a more convenience way to do that is
function addEvent(ele, type, func){
if(ele.addEventListener){
ele.addEventListener(type, func, false);
}else if(ele.attachEvent){
ele.attachEvent("on" + type, func);
}
}
addEvent(document.body, "click", function(){
alert("Hello world!");
});
Using event listner's might do the trick ,but please note that the W3C model does not state which event handler is fired first.
I would better suggest you to check for the server response from within your function and call the second function from there.
Without jQuery, add event listeners instead of using the onClick values. Here's a description.
yourelement.attachEvent('onclick',hello);
Function hello(){
// do wat you want here
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745613754a4636107.html
评论列表(0条)