0

私はjQueryが初めてです。シンプルなページを作成しようとしています。Google がホストする jQuery を使用します。stackoverflow の多くの例に基づいて、この小さなコードを書きました。change イベントが発生することはありません。「読み込めません」という警告も表示されません。firebug を調べて Google のスクリプト タグを展開すると、「未定義」と表示されます。Webサーバーを使用せずに、ローカルでテストしています。誰でも私の間違いを指摘できますか?以下のようにコードします。

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="./styles/qForm.css" rel="stylesheet" type="text/css"/>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
    <script>
        if (!window.jQuery) {
            alert("can not load');
        }

        $('#choice').change(function () {
                    alert('here!');
                    if($(this).val() == '0') $(this).addClass('empty');
                    else $(this).removeClass('empty')
                });

        $('#choice').change();

    </script>

</head>
<body>
    <div id="surround">
        <div id="top">
            <h3 id="title"> Enter Information </h3>
        </div>
        <div id="bottom">
             <select id="choice">
                 <option style="color: gray;" value="0" selected="selected">Select...</option>
                 <option value="1">one</option>
                 <option value="2">two</option>
                 <option value="3">three</option>
             </select>
        </div>
    </div>
</body>
4

4 に答える 4

2

関数の引用符にタイプミスがありalertます。

alert("can not load');

引用符は同じでなければなりません。これが、コードが機能しない最初の理由です。

alert("can not load");
于 2013-10-11T18:21:20.863 に答える
0

http: をスクリプト ソースに追加します。

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
于 2013-10-11T18:20:53.390 に答える
0

jQuery の使用では、「ready」関数でラップするのが一般的です。

<script>
$(document).ready(function(){
    $('#choice').change(function () {
        alert('here!');
    });
});
</script>

これは、バインディングを適用する前に、ページが完全に読み込まれるまで jQuery が待機することを意味します。まだ存在しない要素 (ページがそれらの要素をレンダリングしていない) にバインディングを適用しようとすると、無視されます。

于 2013-10-11T18:20:59.270 に答える
0

<script src="//ajax...."ローカル ファイルを使用している場合、ブラウザは を認識しません。これをに変更<script src="http://ajax...."

于 2013-10-11T18:21:03.917 に答える