gpt4 book ai didi

javascript - 如何正确地从元素中取出文本并销毁元素?

转载 作者:行者123 更新时间:2023-11-30 11:57:28 24 4
gpt4 key购买 nike

JSFIDDLE

我有一个段落标签,里面有 2 个(可以更多)highlight 标签。

我现在想做的是,当我点击按钮时,我想要销毁包含“可移动”文本的 highlight 标签,并替换为没有 highlight 标记和所有 data-* 属性。

HTML:

<p>
<highlight class="highlight" data-id="1464586442243" data-container="body" data-toggle="popover" data-placement="right" data-content="<p>first</p><button class=&quot;btn edit_annotation&quot; data-id=&quot;1464586442243&quot;>Edit</button>&amp;nbsp;<button class=&quot;btn delete_annotation&quot; data-id=&quot;1464586442243&quot;>Delete</button>"
id="anchor_1464586442243" data-original-title="" title="">Section</highlight> 1
<highlight class="highlight" data-id="1464586450092" data-container="body" data-toggle="popover" data-placement="right" data-content="<p>second</p><button class=&quot;btn edit_annotation&quot; data-id=&quot;1464586450092&quot;>Edit</button>&amp;nbsp;<button class=&quot;btn delete_annotation&quot; data-id=&quot;1464586450092&quot;>Delete</button>"
id="anchor_1464586450092" data-original-title="" title="">removable</highlight> true</p>
<button id="remove" type="button">Remove</button>

JS:

$(function() {
$('#remove').click(function() {
// i stuck here
});
});

预期结果:

<p>
<highlight class="highlight" data-id="1464586442243" data-container="body" data-toggle="popover" data-placement="right" data-content="<p>first</p><button class=&quot;btn edit_annotation&quot; data-id=&quot;1464586442243&quot;>Edit</button>&amp;nbsp;<button class=&quot;btn delete_annotation&quot; data-id=&quot;1464586442243&quot;>Delete</button>" id="anchor_1464586442243" data-original-title="" title="">Section</highlight> 1 removable true
</p>

怎么做?我尝试像提到的那样使用 .contents().unwrap() here但它对我不起作用。

这是我使用 .contents().unwrap() 后的当前结果:

<p><highlight class="highlight" data-id="1464586442243" data-container="body" data-toggle="popover" data-placement="right" data-content="<p>first</p><button class=&quot;btn edit_annotation&quot; data-id=&quot;1464586442243&quot;>Edit</button>&amp;nbsp;<button class=&quot;btn delete_annotation&quot; data-id=&quot;1464586442243&quot;>Delete</button>" id="anchor_1464586442243" data-original-title="" title="">Section</highlight> 1 remov<highlight></highlight>able true</p>

最佳答案

我认为 jQueryreplaceWith 方法应该可以帮助您做到这一点。这是工作示例:

$(function() {
$('#remove').click(function() {
// Find all the HIGHLIGHT tags which has text "removable"
var $target = $("highlight:contains('removable')");

// Replace all those instances of HIGHLIGHT tags with the text inside of each of those
$target.replaceWith(function(){
return $(this).text();
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
<highlight class="highlight" data-id="1464586442243" data-container="body" data-toggle="popover" data-placement="right" data-content="<p>first</p><button class=&quot;btn edit_annotation&quot; data-id=&quot;1464586442243&quot;>Edit</button>&amp;nbsp;<button class=&quot;btn delete_annotation&quot; data-id=&quot;1464586442243&quot;>Delete</button>"
id="anchor_1464586442243" data-original-title="" title="">Section</highlight> 1
<highlight class="highlight" data-id="1464586450092" data-container="body" data-toggle="popover" data-placement="right" data-content="<p>second</p><button class=&quot;btn edit_annotation&quot; data-id=&quot;1464586450092&quot;>Edit</button>&amp;nbsp;<button class=&quot;btn delete_annotation&quot; data-id=&quot;1464586450092&quot;>Delete</button>"
id="anchor_1464586450092" data-original-title="" title="">removable</highlight> true</p>
<button id="remove" type="button">Remove</button>

快乐编码...

关于javascript - 如何正确地从元素中取出文本并销毁元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37518350/

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