jquery
2011-06-05 6 views 0 likes 
0

をラップされていない.wrap:は、私がこれを書いた

<html> 
    <head> 

    <script type="text/javascript" src="jquery.js"></script> 
    <script> jQuery(document).ready(function($) 
    { $("div.productInfo").wrap("<div id='productDetails' />"); 
      $("ul.productInfo").wrap("<div id='specs' />"); 
     $("#centerColumn" + "#rightColumn").wrap("<div id='test' />"); 
    }); 
    </script> 


    </head> 
    <body> 
    <p id="centerColumn" >This is a paragraph.</p> 
    <p id="rightColumn" >This is another paragraph.</p> 
    <div class="productInfo" > Wow </div> 

    </body> 
    </html> 

をそして#centerColumnは#rightColumn取得するのは、ラップラップされません、なぜですか?

答えて

0
<html> 
<head> 
    <script src="jquery.js"></script> 
    <script> 
     $(function() { 
      $("div.productInfo") 
       .wrap("<div id='productDetails'><div id='specs' /></div>"); 
      $("#centerColumn, #rightColumn").wrapAll("<div id='test' />"); 
      // or .wrap() instead of .wrapAll() depends on your intentions 
     }); 
    </script> 
</head> 
<body> 
    <p id="centerColumn">This is a paragraph.</p> 
    <p id="rightColumn">This is another paragraph.</p> 
    <div class="productInfo"> Wow </div> 
</body> 
</html> 

参照:

  • .wrap() jQueryのドキュメント
3

あなたはこのセレクタを使用している:

$("#centerColumn" + "#rightColumn") 

+連結演算子があります。文字列を結合するので、コードは次のようになります。

$("#centerColumn#rightColumn") 

これは明らかに役に立たないでしょう。

私はあなたがmultiple selectorをしたいと思う:

$("#centerColumn, #rightColumn").wrap("<div id='test' />"); 

あなたは同じ祖先要素で両方の列をラップしたい場合は、wrapAllが必要になります。これはおそらく、ラップ要素にididという属性が一意である必要があるためです。

$("#centerColumn, #rightColumn").wrapAll("<div id='test' />"); 
+0

おかげで、jQueryのAPI

  • SelectorsでjQueryのAPI
  • .wrapAll()でそれが行われています –

  • 0

    おそらく、このため:

    $("#centerColumn" + "#rightColumn").wrap("<div id='test' />"); 
    

    は、このことが必要です。また、

    $("#centerColumn,#rightColumn").wrap("<div id='test' />"); 
    
    関連する問題