gpt4 book ai didi

javascript - jquery not() 函数不适用于所有元素

转载 作者:行者123 更新时间:2023-12-03 12:12:42 25 4
gpt4 key购买 nike

我使用jquery不删除元素的html。我没有让 jQuery not() 以正确的方式工作。它会删除子元素中除我提到的最后一个元素之外的所有元素。

我的 HTML-

<body>
<div id="edit-save-section">
<button id="edit-button" class="btn btn-info btn-large">
<span><i class="fa fa-cog"></i>&nbsp;EDIT</span>
</button>
<button class="division-twenty btn btn-success btn-large" data-loading-text="Saving..." class="btn btn-primary" id="btn_save"><span><i class="fa fa-save"></i>&nbsp;SAVE</span>
</button>
</div>

<nav id="menu">
<ul>
<li><a href="javascript:;">Link1</a>
</li>
<li><a href="javascript:;">Link2</a>
</li>
<li><a href="javascript:;">Link3</a>
</li>
<li><a href="javascript:;">Link4</a>
</li>
</ul>
</nav>

<div class="removable-section">

<a href="javascript:;" class=" pull-right span1 handle" rel="tooltip" title="Arrange section" data-placement="left"><b><i class="fa fa-arrows fa-2x"></i></b></a>
<a class="span1 pull-right remove-section-action" rel="tooltip" title="Remove section" data-placement="left" href="javascript:;"><b><i class="fa fa-times fa-2x"></i></b></a>
</div>

<div id="allhtml"></div>
</body>

使用 jQuery 清除 html-

 $('#btn_save').click(function () {
var notAll = $('body').children().not("#menu", ".removable-section", "#edit-save-section").html();
$('#allhtml').append(notAll);
console.log($('#allhtml').html());
});

我设法删除了 #menu.removable-section ,但我无法删除 #edit-save-section

如果我尝试更改这些部分的顺序,例如 -

 var notAll = $('body').children().not("#menu","#edit-save-section",".removable-section").html();

然后它的行为就很奇怪。它不会从正文中删除上述 html 结构中找到的最后一个子级。在本例中,它是 .removable-section

请告诉我我做错了什么?

最佳答案

您还可以使用 JQuery Clone 来实现此目的

$('#btn_save').click(function () {
var notAll = $('body').clone(true);
notAll.find("#menu").remove();
notAll.find(".removable-section").remove();
notAll.find("#edit-save-section").remove();
$('#allhtml').append(notAll);

});

关于javascript - jquery not() 函数不适用于所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24885153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com