gpt4 book ai didi

javascript - jquery 从 html 方法中排除元素

转载 作者:行者123 更新时间:2023-11-28 02:48:53 27 4
gpt4 key购买 nike

如何选择嵌套在<div class="thewrapper">下的所有元素的html除了那些有 class="excludeme"的?

<div class="thewrapper">
<div class="excludeme"> some content to exclude.... </div>

<div class="whatever">
<span> lots of content and tags </span>
</div>

<div class="excludeme"> some content to exclude again.... </div>

<div class="whatever">
<span> more content and tags </span>
</div>

我一直在摆弄 jquery .not 和 :not 运算符,但无法使其正常工作。例如。以下不起作用:

var pagehtml = $("div[class^='thewrapper']:not(div[class^='excludeme']").html();
var pagehtml = $('div[class^="thewrapper"]').not('[class="excludeme"]').html();
var pagehtml = $("div.thewrapper:not(:has(div.excludeme))").html();
var pagehtml = $('div:not(".excludeme")').html();

最佳答案

您可以使用这个微型 jQuery 插件 I've created for a similar Question轻松获取排除/忽略特定选择器的元素的内容(文本或 html):

$.fn.ignore = function(sel){
return this.clone().find(sel||">*").remove().end();
};

var notExcludeme = $(".thewrapper").ignore(".excludeme").html();
console.log(notExcludeme);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="thewrapper">
<div class="excludeme"> some content to exclude.... </div>

<div class="whatever">
<span> lots of content and tags </span>
</div>

<div class="excludeme"> some content to exclude again.... </div>

<div class="whatever">
<span> more content and tags </span>
</div>

</div>

关于javascript - jquery 从 html 方法中排除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311768/

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