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 に答える