1

jqueryを使用する共有javascriptファイルがあり、それをcommon.jsと呼び、 hello.htmlworld.htmlという2つのページで使用されます。スクリプトには、次のイベント ハンドラーが含まれています。

$("#helloPageButton").click(function () { ... });
$("#worldPageButton").click(function () { ... });

これは Chrome、Firefox ではすべて正常に動作しますが、IE では hello.html はこれらのセレクターの行で「オブジェクトが必要です」というエラーをスローします。

IE がエラーをスローせず、セレクターが見つからない場合、つまり null の場合に先に進むにはどうすればよいですか?

別の例を次に示します。これは IE では機能しません。

<html>
<body>

<input type="button" id="helloButtonID" value="This is the hello button">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script>
$("#worldButtonID").click(function(){
     alert("The world button is not on the webapge.");   
});
</script>

</body>
</html>
4

1 に答える 1