我写了以下内容:
<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 get,为什么?
发布于 2011-06-06 07:47:52
<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>另请参阅:
发布于 2011-06-06 07:44:38
您正在使用此选择器:
$("#centerColumn" + "#rightColumn")那里的+是连接运算符。它连接字符串,因此您的代码与以下代码相同:
$("#centerColumn#rightColumn")这显然找不到任何有用的东西。
我觉得你想要multiple selector
$("#centerColumn, #rightColumn").wrap("<div id='test' />");如果希望将这两个列包装在同一祖先元素中,则需要wrapAll。这似乎是可能的,因为您为包装元素提供了一个id,并且id属性必须是唯一的。
$("#centerColumn, #rightColumn").wrapAll("<div id='test' />");发布于 2011-06-06 07:44:50
也许是因为:
$("#centerColumn" + "#rightColumn").wrap("<div id='test' />");应该是这样:
$("#centerColumn,#rightColumn").wrap("<div id='test' />");https://stackoverflow.com/questions/6246670
复制相似问题