2012-01-17 3 views
0

divを項目の周りに動的に追加し、2番目のビットを実行して項目を回転させる必要があります。jQuery jqueryを動的に追加してから2番目の呼び出しを実行する方法

これまでのところdivを追加できますが、2番目の部分は必要なように読み込まれません。

<div id="ContentFeed"> 
<div class="newsItem">Text</div> 
<div class="newsItem">Text</div> 
<div class="newsItem">Text</div> 
<div class="newsItem">Text</div> 
<div class="newsItem">Text</div> 
</div> 

<script>$('#ContentFeed .newsItem').wrapAll('<div id="slider" />');</script> 


    <div id="ContentFeed"> 
    <div id="slider"> 
     <div class="newsItem">Text</div> 
     <div class="newsItem">Text</div> 
     <div class="newsItem">Text</div> 
     <div class="newsItem">Text</div> 
     <div class="newsItem">Text</div> 
    </div> 
    </div> 

<script> 
$(function(){ 
    $('#slider').bxSlider({ 
     auto: false, 
     displaySlideQty: 6, 
     moveSlideQty: 6, 
     pagerType: short, 
     pager: true 
    }); 
}); 
</script> 

任意のアイデアはどのように動的に追加のdivを見るために、第2の部分を取得しますか?

答えて

1

あなたは同じdocument.readyイベントハンドラでjQueryのコードの最初のビットをラッピングしてみてください、あまりにも早くダイナミックdiv要素を追加しようとすることができる。

$(function(){ 
    $('#ContentFeed .newsItem').wrapAll('<div id="slider" />'); 
    $('#slider').bxSlider({ 
     auto   : false, 
     displaySlideQty : 6, 
     moveSlideQty : 6, 
     pagerType  : short, 
     pager   : true 
    }); 
}); 

これはDOMを検査することにより、問題である場合は、確認することができます#slider要素が実際にDOMに追加されているかどうかを確認します。

+0

私はそれが孤立した場合に、これが動作するため、実行をブロックしている別のエラーがあるかもしれません – Ashlee

+0

@Ashleeを働かなかったことを試してみました。ここにデモがあります:http://jsfiddle.net/JZLEy/。エラーへのリンクを投稿できますか? – Jasper

+0

これをもう一度見て、私は間違ったアイテムを呼んでいたことに気付きました。それは今働いている!ありがとう – Ashlee

0

.on方法

$(document).on("ready", function(){ your slider code... のようなものを使ってみて、おそらくあまりにもその中の他のタラを包みます。

ここでそれについての記事を読む

http://api.jquery.com/on/

+0

'.on()'はjQuery 1.7でイベントをバインドする新しい方法ですが、 '$(document).on(" ready "、function(){/ * code here * /});'は同じものです'$(function(){/ * code here * /}); ' – Jasper

+0

もっと具体的にすることができますか? – Ashlee

+0

hmm。私はまだライブ関数を使うのに多少慣れていますが、それは廃止されました。アシュリー・ジャスパーズ。それはちょうどあなたのコードの順序でなければなりません。あなたのコンソールthrowinエラーですか? – Rooster

関連する問題