开发者

.wrap not being wrapped

I wrote this:

   <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>

And the #centerColumn doesn't get wrapped only the #right开发者_如何学运维Column get's wrapped, why?


You're using this selector:

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

The + there is the concatenation operator. It joins the strings, so your code is the same as this:

$("#centerColumn#rightColumn")

This will obviously find nothing useful.

I think you want the multiple selector:

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

If you want to wrap both columns in the same ancestor element, you'll need wrapAll. This seems likely because you are giving your wrapping element an id and id attributes must be unique.

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


<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>

See also:

  • .wrap() at jQuery API
  • .wrapAll() at jQuery API
  • Selectors at jQuery Docs


Perhaps because this:

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

Should be this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜