如何处理JavaScript中的点击链接? [英] How to handle a clicked link in JavaScript?
本文介绍了如何处理JavaScript中的点击链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何处理没有ID的链接?它只有一个类名classbeauty。
How can I handle a link which doesn't have an id? It just has a classname like "classbeauty".
现在我需要知道用户是否点击了链接。
如果点击链接,我只需要拨打 alert(是链接点击);
。
Now I need to know if a user has clicked the link.
If the link is clicked I just need to call alert("yes link clicked");
.
我不知道如何处理JavaScript中的事件。
I don't know how to handle events in JavaScript.
我该怎么做?
推荐答案
如果jQuery是一个选项:
If jQuery is an option:
$("a.classbeauty").click(function(){
alert("yes link clicked");
});
如果您需要纯JavaScript:
If you need pure JavaScript:
var elements = document.getElementsByTagName("a");
for(var i=0; i<elements.length; i++){
if (elements[i].className == 'classbeauty') {
elements[i].onclick = function(){
alert("yes link clicked!");
}
}
}
如果您需要< a href =http://en.wikipedia.org/wiki/Greasemonkey =nofollow noreferrer> Greasemonkey :
function GM_wait() {
if(typeof unsafeWindow.jQuery != 'function') {
window.setTimeout(wait, 100);
} else {
unsafeWindow.jQuery('a.classbeauty').click(function(){
alert('yes link clicked');
});
}
}
GM_wait();
这篇关于如何处理JavaScript中的点击链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文