2011-07-25 13 views
4

私はdiv id(オークション)を持っていますが、私はオークションリストの子とHTMLを入れ替えたいと思います。しかし、jQueryはオークションリストのdivの子を選択することはできません。ここでjQueryのnth-childセレクタが動作しない

はHTMLです:

<div id="all">  
    <div id="auctions"></div> 
</div> 

<div id="auction-list" class="hide"> 
    <div class="auction">Test</div> 
    <div class="auction">Test</div> 
</div> 

はここでjQueryの:

まずアラート

2 

alert($("#auction-list").children().length); 
alert($("#auction-list").html()); 
alert($("#auction-list:nth-child(1)").html()); 
alert($("#auction-list:nth-child(2)").html()); 
$("#auctions").html($("#auction-list:nth-child(1)").html()); 

そして、ここではJavascriptがアラートの出力であります

セカンドアラート

<div class="auction">Test</div> 
<div class="auction">Test</div> 

サードアラート

null 

私はここで何を望むのですか?

答えて

19

あなたはこのように、あなたのセレクタの間のスペースを必要とする:あなたが実際に探しているものをするとき、あなたのセレクタで

alert($("#auction-list :nth-child(1)").html()); 
//     ^-- Space here 

、それは、他の要素の第一子である要素#auction-list、探していますリストのn番目の子である要素に対して

+0

これはそれです。私はコロンの前にスペースが必要でした。 – Jarred

0

は、以下のことを試してみてください。

$("#auction-list > .auction:nth-child(2)") 

docsを見てみましょう。子要素の代わりにn番目の#auction-list要素を選択しようとしています。

0
alert($("#auction-list").children().length); 
alert($("#auction-list").html()); 
alert($("#auction-list div:nth-child(1)").html()); 
alert($("#auction-list div:nth-child(2)").html()); 
$("#auctions").html($("#auction-list:nth-child(1)").html()); 
2

要素IDに:nth-childセレクタを使用する必要はありません。それは分類された項目でなければなりません

のような;

alert($("div.auction-item:nth-child(1)").html()); 
0

私は、クリックイベントがIEとChromeではうまくいきますが、Firefoxではうまくいきませんでした。私はタイプミスを見過ごしていました。それが誰かを助ける場合に備えて分かち合うと思った。

$(document).ready(function() { 
    $(".test table tr:nth-child(1n+2").click(function() { 
      alert(this.id); 
     }); 
}); 

+2以降が欠落していました。

JSFiddle:https://jsfiddle.net/xv3bca60/7/

関連する問題