admin管理员组文章数量:1433520
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
}
本文标签: javascriptextend onclick functionStack Overflow
版权声明:本文标题:javascript - extend onclick function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745613754a2666272.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论