1

奇妙な問題が発生しています.ajaxを使用して動的に生成されるリスト項目にホバー機能を追加しましたが、何も起こっていません.コードはエラーなしで実行されていますが、効果はありません.mouseenterとmouseoutのアラートも表示されません.アラートは時々ポップアップしますが、毎回ではありません。次のコードを使用しています。

$('.category_list li').live("mouseenter", function() { 
alert('I m here');
  $(this).find('.category_list').css('text-decoration','underline');
}).live("mouseleave", function() {
alert('I m gone');
  $(this).find('.category_list').css('text-decoration','none');
}); 

私のhtmlコードは

htmlData.push('<ul class="category_list">');
htmlData.push('<li><a href="javascript:void(0);" onclick="callGetApplicationDetails('+iIndex+',0);" >'+categoryName+'</a></li>');

私は非常にひどく立ち往生しているので、私を助けてください。

ありがとうヘミッシュ

4

3 に答える 3

2

mouseoverandmouseoutイベントを使用してみてください。<li>フィルタリングセレクターが親の要素を探していたと思いますか?

$('.category_list li').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
        alert('I m here');
        $(this).parent().css('text-decoration','underline');
    } else {
        alert('I m gone');
        $(this).parent().css('text-decoration','none');
    }
});

そして、おそらく新しいcssクラスでjQueryを少しクリーンアップすることができます

.category_list.over
{
    text-decoration: underline;
}

と使用toggleClass()

$('.category_list li').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') { alert('I m here'); } 
    else { alert('I m gone'); }

    $(this).parent().toggleClass('over');
});
于 2011-02-25T18:20:17.223 に答える
0

この機能で IE6 をサポートする必要がない場合は、代わりに CSS を使用してください。

例: http://jsfiddle.net/QLsQp/

.category_list li a {
    text-decoration:none;
}

.category_list li:hover a {
    text-decoration:underline;
}

これは、:hover疑似セレクターを使用してa、a がホバーされたときにネストされた要素に影響を与えliます。


あなたのjavascriptの問題は、これです:

$(this).find('.category_list')

... は要素.category_list祖先であり、子孫ではないため、何も見つかりません。<li>

代わりにこれが必要です:

$(this).find('a')
于 2011-02-25T18:24:15.307 に答える
0

ついに!私はライブクエリを使用しましたが、うまくいきました!

$('.category_list li').livequery("mouseenter", function() {
    $(this).css({'background-color' : '#A9A8A8'})
}).livequery("mouseleave", function() {
    $(this).css({'background-color' : '#F4F4F4'})
});

@パトリック:助けてくれてありがとう。

それが他の人にも役立つことを願っています。

于 2011-02-25T21:43:40.487 に答える