4

我目前正在addEventListener使用select

<select>
    <option value="Apple">Mac</option>
    <option value="Microsoft">Windows</option>
</select>

document.querySelector("select").addEventListener("change",function(ev){
    //do something here...
}, false);

但我不知道如何从event.

PS:我不想这样做:

<select onchange="func(event,this)">

这只会弄乱HTML ...

4

1 回答 1

2

导致事件的元素this在事件处理程序中自动分配。它也在传递给addEventListener()回调的事件数据结构中,在您的示例中是ev.target.

document.querySelector("select").addEventListener("change",function(ev){
    //do something here...
    // you can use the value of this to access the object that 
    //    is responding to the event
    // you can also look in the event data structure for 
    //    other items like ev.target
}, false);

您可以在此处查看事件对象的所有成员。

于 2012-05-27T04:37:22.537 回答