简体   繁体   English

你如何用普通的 Javascript 捕捉点击事件?

[英]How do you catch a click event with plain Javascript?

I know that when using jQuery you can do $('element').click();我知道在使用 jQuery 时你可以做$('element').click(); to catch the click event of an HTML element, but how do you do that with plain Javascript?捕捉 HTML 元素的点击事件,但是如何使用普通的 Javascript 做到这一点?

document.getElementById('element').onclick = function(e){
  alert('click');
}

DEMO: http://jsfiddle.net/e9jZW/1/演示: http : //jsfiddle.net/e9jZW/1/

By adding an event listener or setting the onclick handler of an element:通过添加 事件侦听器或设置元素的onclick处理程序

var el = document.getElementById("myelement");

el.addEventListener('click', function() {
  alert("Clicked");
});

// ... or ...

el.onclick = function() {
  alert("Clicked");
}

Note that the even listener style allows multiple listeners to be added whereas the callback handler style is exclusive (there can be only one).请注意,偶数侦听器样式允许添加多个侦听器,而回调处理程序样式是独占的(只能有一个)。

If you need to add these handlers to multiple elements then you must acquire them as appropriate and add them to each one separately.如果您需要将这些处理程序添加到多个元素,那么您必须根据需要获取它们并将它们分别添加到每个元素中。

I usaly create a kind of global event handler, very powerful, you can capture className, of the event "types" nodeTypes, you name it, i hope people will find this useul我通常创建一种全局事件处理程序,非常强大,您可以捕获事件“类型”nodeTypes 的 className,您命名它,我希望人们会发现这个有用

document.onclick = eventRef

function eventRef(evt) {
    var han;
    evt || (evt = window.event);

    if (evt) {
        var elem = evt.target ? han = evt.target : evt.srcElement && (han = evt.srcElement);

         // evt.type could be, mouseup, mousedown...
        // elem.id is the id or the element
        // elem.className is the class name of the element
        // you could nest expression, use substrings to extract part of a className
        if (evt.type=="click" && elem.id == "gotit" || elem.className == "someClassName") {  
            alert(elem.id);
        }
    }
}

Thanks to vanilla js感谢香草js

document.addEventListener('click', function (event) {

// If the clicked element doesn't have the right selector, bail
if (!event.target.matches('input')) return;

// Don't follow the link
event.preventDefault();

// Log the clicked element in the console
console.log(event.target);

}, false);

This also show which element is c这也显示哪个元素是 c

I would recommend going with addEventListener instead of assigning the handler function directly.我建议使用addEventListener而不是直接分配处理程序函数。

var div = document.getElementById('test');
div.addEventListener('click', function(){ 
    console.log('CLICKED');
});

There are several reasons for that by I am going to name those I find the most important:我将列出我认为最重要的原因有几个原因:

  1. You can't mistakenly add event listener to a non-DOM object with addEventListener - your code would fail instead of quietly assigning onclick function to some object您不能使用addEventListener错误地将事件侦听器添加到非 DOM 对象 - 您的代码将失败,而不是悄悄地将onclick函数分配给某个对象
  2. You can attach only one (without additional code manipulation for every handler that you want to add) event listener with onclick - something that might prove limiting只能使用onclick附加一个(无需为要添加的每个处理程序进行额外的代码操作)事件侦听器 - 这可能会受到限制

Here is an interesting article about that, i used the same approach some times, basically, you add the event handler to window .这是一篇关于此的有趣文章,我有时使用相同的方法,基本上,您将事件处理程序添加到window http://mislav.uniqpath.com/js/handling-events-on-elements/ http://mislav.uniqpath.com/js/handling-events-on-elements/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM