0

これはおそらく初心者の質問ですが、以下の appendTo() 関数を実装した方法は期待どおりに機能しません。基本的に、要素を追加してすぐに削除しました。それはまばたきであり、あなたはそれを見逃す.

なぜこれが起こっているのか誰にも理解できますか?

関数が呼び出される場所は次のとおりです。

<?php foreach ($words as $word) {
echo    "<li class='$word[0]'><a href='' onclick='add_to();'>$word</a></li>";
} 

関数自体は次のとおりです (ほとんど jQuery チュートリアル サイトから引用したものです。

function add_to () {
        $('<h1>Test</h1>').appendTo('.ad_text');
    }

私の最初の考えは、document.ready() を呼び出すスクリプトが呼び出され、add_to() 関数を一掃するということですか? そのスクリプトは add_to() の上にあり、これは次のとおりです。

$(document).ready(function(){

        //when a link in the filters div is clicked...
        $('#filters a').click(function(e){

            //prevent the default behaviour of the link
            e.preventDefault();

            //get the id of the clicked link(which is equal to classes of our content
            var filter = $(this).attr('id');

            //show all the list items(this is needed to get the hidden ones shown)
            $('#content ul li').show();

            /*using the :not attribute and the filter class in it we are selecting
            only the list items that don't have that class and hide them '*/
            $('#content ul li:not(.' + filter + ')').hide();

        });

    });

おそらくそこに競合するコードがありますか?申し訳ありませんが、Javascript は初めてで、何かをすばやくまとめようとしています。

ティア、アンディ

4

2 に答える 2

5

リンクがページをリロードしています。これを試してください(href属性に#を追加)

foreach ($words as $word) {
    echo    "<li class='$word[0]'><a href='#' onclick='add_to();'>$word</a></li>";
}
于 2010-07-27T10:18:55.320 に答える
0

確かなことは言えませんが、onclick 属性で定義したクリック関数を document.ready 関数でオーバーライドしている可能性があります。

于 2010-07-27T10:22:37.063 に答える