简体   繁体   English

jQuery wrap和appendTo没有导致预期的结果

[英]jQuery wrap and appendTo doesn't lead in expected result

I'm trying to wrap some HTML content inside a div container and then append a span to that container. 我正在尝试将一些HTML内容包装在div容器中,然后将span添加到该容器中。 But I can't figure out why the appended span doesn't appear. 但我无法弄清楚为什么没有出现附加的跨度。

So I'm curious if I'm doing something wrong. 所以我很好奇我是否做错了什么。

 $('button').click(function() { var $container = $('<div class="container"></div>'); $('.target').wrap($container); $label = $('<span class="duration-label">Hi there</span>'); $label.appendTo($container); }); 
 .target { border: 1px solid black; } .container { background: red; padding: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <div class="target"> <span>This is some unimportant text</span> </div> <button type="button">Click me</button> 

It's because wrap makes a copy of the wrapper you give it, it doesn't use the one you give it directly. 这是因为wrap会为你提供的包装器提供一个副本 ,它不会直接使用你提供的包装器。 You're appending to the one you gave it. 你附加了你给它的那个。

Instead, use the one you wrapped around the content: 相反,使用围绕内容包裹的那个:

 $('button').click(function() { // Wrap .target with the div, and get a reference to // the newly-created wrapper var $container = $('.target') .wrap('<div class="container"></div>') .parent(); // Add a label to it var $label = $('<span class="duration-label">Hi there</span>'); $label.appendTo($container); }); 
 .target { border: 1px solid black; } .container { background: red; padding: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <div class="target"> <span>This is some unimportant text</span> </div> <button type="button">Click me</button> 


Side note: Your code was falling prey to The Horror of Implicit Globals because you didn't declare $label . 旁注:你的代码正在成为Implicit Globals的恐怖的牺牲品,因为你没有声明$label It's important to declare your variables. 声明变量很重要。 You can use strict mode to have the JavaScript engine keep you from creating globals implicitly. 您可以使用严格模式让JavaScript引擎隐藏无法创建全局变量。

That is because $container refers to variable and not object of wrapped element. 这是因为$container指的是变量而不是包装元素的对象。 You need to get the object or use relevant selector to append to container element : 您需要获取对象或使用相关选择器追加到容器元素:

$label.appendTo('.container');

Working Demo 工作演示

The reason is that the .wrap() method wraps each element in the set of matched elements with a new element structure - copies of $container . 原因是.wrap()方法用一个新的元素结构包装匹配元素集中的每个元素 - $container副本。

$container itself is not the wrapping element, it's just a "template" for wrapping elements, so you need to grab the actual element(s) if you want to further manipulate them. $container本身不是包装元素,它只是包装元素的“模板”,所以如果你想进一步操作它们,你需要抓住实际的元素。

This of course is simple to understand once you consider the fact that after the call there can be more then 1 such element. 当你考虑到在调用之后可以有超过1个这样的元素时,这当然很容易理解。

 $('button').click(function() { var $container = $('<div class="container"></div>'); var $wrappingContainer = $('.target').wrap($container).parent(); // sanity check: if($container[0] == $wrappingContainer[0]) { alert('Same object?!?!'); // Should not happen } $label = $('<span class="duration-label">Hi there</span>'); $label.appendTo($wrappingContainer); }); 
 .target { border: 1px solid black; } .container { background: red; padding: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <div class="target"> <span>This is some unimportant text</span> </div> <button type="button">Click me</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM