将元素换行到新的div

时间:2017-08-03 11:49:21

标签: jquery

我有以下代码:

var searchInput = '<md-input-container md-theme-watch="true" flex><label for="sampletext1">Champ texte</label><input name="sampletext1" type="text" class="ng-tree-search"></md-input-container>';
element.before($compile(searchInput)(scope))
    .prev()
    .on('keyup', function (ev) {
        if (to) {
            clearTimeout(to);
        }
        to = setTimeout(function () {
            tree.jstree(true).search(ev.target.value);
        }, 250);
    });

我想将element包装到div:<div class="nrh-tree-search-container"></div>,并将我使用before创建的另一个元素包装到与第一个子节点相同的div中,所以它看起来像这样:

<div class="nrh-tree-search-container">
   <md-input-container md-theme-watch="true" flex><label for="sampletext1">Champ texte</label><input name="sampletext1" type="text" class="ng-tree-search"></md-input-container>
<!-- element should be added here -->
</div>

我尝试了以下内容:

var searchInput = '<md-input-container md-theme-watch="true" flex><label for="sampletext1">Champ texte</label><input name="sampletext1" type="text" class="ng-tree-search"></md-input-container>';
var searchForm = element.before($compile(searchInput)(scope))
    .prev()
    .on('keyup', function (ev) {
        if (to) {
            clearTimeout(to);
        }
        to = setTimeout(function () {
            tree.jstree(true).search(ev.target.value);
        }, 250);
    });
searchForm.wrap($compile('<div class="nrh-tree-search-container"></div>')(scope));

但是这不起作用我在element之外得到'<div class="nrh-tree-search-container"></div>'

我该如何解决这个问题?

编辑:

演示http://jsfiddle.net/vwmHF/121/

1 个答案:

答案 0 :(得分:1)

你应该这样试试:

检查.wrapAll()方法:

$('.accordionTrigger p').wrapAll('<div class="moreInfo"></div>');

wrapAll()方法将所有匹配的元素包装到另一个元素中(与.wrap()方法相比,后者单独包装匹配的元素)

<强> DEMO