2015-02-24 132 views
1

我有一個按鈕狀波紋管querySelector()的attachEvent()不工作

<input type="button" value="check" /> 

而且,我嘗試在此按鈕單擊通知如下

document.querySelector('input[type="button"]').attachEvent('onclick', function() { alert(1); }); 

但是,這是行不通的,爲什麼?

+2

'attachEvent'只適用於IE瀏覽器!您可能正在尋找['addEventListener'](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener)方法。 – undefined 2015-02-24 07:04:59

+0

如果瀏覽器不是IE瀏覽器,則可以使用addEventListener''document.querySelector('input [type =「button」]')。addEventListener('click',function(){alert(1);}); ' – 2015-02-24 07:08:51

+0

爲什麼不能使用jQuery – 2015-02-24 07:09:24

回答

1

使用此代碼片段: -

document.querySelector('input[type="button"]').addEventListener('click', function() { alert(1); }); 
相關問題