Der*_*會功夫 5 javascript events addeventlistener
我目前正在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);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何从 中获取原始元素event。
PS:我不想这样做:
<select onchange="func(event,this)">
Run Code Online (Sandbox Code Playgroud)
这只会弄乱 HTML...
引发事件的元素会自动分配到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);
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看事件对象的所有成员。