简体   繁体   English

jQuery的`包装`不适用于附加

[英]JQuery `wrap` not working with append

I have a function that is suppose to return several HTML elements wrapped in span tags. 我有一个函数应该返回几个包裹在span标签中的HTML元素。 The elements are returned, by they are not wrapped in the tag. 返回元素,因为它们没有包装在标记中。

This is my code: 这是我的代码:

function summerizeEdit(edit) {
    var editHtmlBody = '<div>' + edit.content + '</div>';
    var $editElements = $(editHtmlBody).find(EDIT_SELECTORS_TO_SUMMERIZE.join(', '));
    var $editSummery = $('<div />').append($editElements.wrap('<span class="test"/>'));
    return $editSummery.html();
}

The jQuery .wrap method wraps the selected elements, and then returns the selected elements. jQuery .wrap方法包装选定的元素,然后返回选定的元素。 Therefore, you'll need to select the wrapper if you want the wrapper to be appended rather than the selected elements. 因此,如果要附加包装器而不是选定的元素,则需要选择包装器。

$('<div>').append(el.wrap('<span>').parent());

 $("#target").append($("<div>").wrap("<div class='test'></div>").text("Hello World!").parent()); 
 .test { color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="target"></div> 

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

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