[英]How to get the wrapper element created with “wrapAll()”?
Consider the following code: (live example here ) 请考虑以下代码:( 这里的实例)
$(function() { var wrapper = $("<div class='wrapper'></div>"); $(".a").wrapAll(wrapper); wrapper.css("border", "5px solid black"); // Doesn't work });
.wrapper { background-color: #777; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="a">Hello</div> <div class="a">Stack</div> <div class="a">Overflow</div>
What would be the right way to get the created wrapper and change its attributes ? 获取创建的包装器并更改其属性的正确方法是什么?
Note: There are other .wrapper
elements in the DOM, so this won't work: 注意: DOM中还有其他
.wrapper
元素,因此不起作用:
$(".wrapper").css("border", "5px solid black");
I don't want to give a unique id
to the created wrapper either. 我也不想给创建的包装器赋一个唯一的
id
。
存储在wrapper
的jQuery对象在调用wrapAll
时被克隆,因此您不能通过操作wrapper
来影响已插入DOM的.wrappers
,您需要从文档中选择它们。
$(function() {
var wrapper = $("<div class='wrapper'></div>");
var wrapped = $(".a").wrapAll(wrapper);
wrapped.css("border", "5px solid black");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.