2010-12-27 4 views
0

私はコメントのリストを表示/非表示にするためにトグルjQueryスクリプト(下記参照)を使用しようとしています。スクリプトは無限のスクロール機能を実装した後、前の投稿の "showtext"を繰り返します(すなわち、無限のスクロールを持つ次の投稿の3つの読み込みの後に "(show)"の3つのコピーがあります) 。無限のスクロールでjqueryトグルを使用する

私はこれがスクリプトの「追加」行のためであることを認識していますが、これをコメントアウトしてclass = "toggleLink"のページにリンクを張ると、コンテンツは切り替わりません。リンクのテキストはhideTextに変わるので、なぜコンテンツが表示されないのか不思議です。助けてくれてありがとう!

$(document).ready(function() { 

// choose text for the show/hide link - can contain HTML (e.g. an image) 
var showText='show'; 
var hideText='hide'; 

// initialise the visibility check 
var is_visible = false; 

// append show/hide links to the element directly preceding the element with a class of "toggle" 
$('.toggle').prev().append(' (<a href="#" class="toggleLink">'+showText+'</a>)'); 

// hide all of the elements with a class of 'toggle' 
$('.toggle').hide(); 

// capture clicks on the toggle links 
$('a.toggleLink').click(function() { 

// switch visibility 
is_visible = !is_visible; 

// change the link depending on whether the element is shown or hidden 
$(this).html((!is_visible) ? showText : hideText); 

// toggle the display - uncomment the next line for a basic "accordion" style 
//$('.toggle').hide();$('a.toggleLink').html(showText); 
$(this).parent().next('.toggle').toggle('slow'); 

// return false so any link destination is not followed 
return false; 

}); 
}); 
+0

あなたのjQueryバージョンによっては、あなたが追加したり削除した要素にバインドする際に便利です(http://api.jquery.com/live/)。 DOM。 –

答えて

1

この問題の回避策を作成しました。それは物事を行う最も効率的な方法ではないかもしれませんが。

はAPPEND()線の上にこの行を追加します:無限スクロールが実行さはshowTextを再挿入することができトグルスクリプトを呼び出すとき(重複を排除する)ように

$('.toggleLink').remove(); 

これは、すべてのtoggleLink要素を削除します。

関連する問題