如何處理 Internet Explorer,以及在 JavaScript 中新增事件偵聽器問題“物件不支援此屬性或方法”嗎?


要解決在 JavaScript 中使用事件和 Internet Explorer 時“物件不支援此屬性或方法”問題,請使用以下程式碼更新您的程式碼 −

示例

<html>
   <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge;" />
   </head>
   <body>
   ...
   </body>
</html>

您還可以在 IE 中使用 attachEvent 解決此問題,如下所示 −

if (ev.addEventListener) {
   ev.addEventListener('click', myText, false);
}
else if (ev.attachEvent) {
    ev.attachEvent('onclick', myText);
}

更新於:23-6-2020

1K+ 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.