jQuery —删除展开的文本但保留元素?

发布于 2024-11-01 06:14:30 字数 325 浏览 12 评论 0原文

所以这是代码:

<parent><child>i want to keep the child</child>Some text I want to remove</parent>

我想将其更改为:

<parent><child>i want to keep the child</child></parent>

我看到很多人询问如何删除子元素并保留展开的文本,但不是相反。你如何瞄准那些没有命名的东西?

So here's the code:

<parent><child>i want to keep the child</child>Some text I want to remove</parent>

I'd like to change it to this:

<parent><child>i want to keep the child</child></parent>

I see lots of people asking how to remove the child element and preserve the unwrapped text, but not the other way around. How do you target something that isn't named?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失退 2024-11-08 06:14:30

试试这个:

var child = $('parent').children('child');

$('parent').html(child);

小提琴:http://jsfiddle.net/maniator/t2CDk/

如果你这样做丢失元素中的所有事件侦听器和数据。

try this:

var child = $('parent').children('child');

$('parent').html(child);

fiddle: http://jsfiddle.net/maniator/t2CDk/

if you do this you lose any event listeners and data from the elements.

时光无声 2024-11-08 06:14:30

您可以使用老式的 javascript 方式来完成此操作,并测试每个节点的 nodeType 为 3,然后对其执行removeChild。这将非常干净和快速(最小的 jQuery),并且不会扰乱任何事件侦听器。

var parent = $('parent')[0];  // Get reference to DOM

for( var i = 0; i < parent.childNodes.length; i++ ) {
  var current_child = parent.childNodes[i];
  if( current_child.nodeType == 3 )
    parent.removeChild( current_child );
}

You can do it the ol' fashioned javascript way, and test each node for the nodeType of 3, then do a removeChild on it. This would be pretty clean and quick (minimal jQuery), and wouldn't mess up any event listeners.

var parent = $('parent')[0];  // Get reference to DOM

for( var i = 0; i < parent.childNodes.length; i++ ) {
  var current_child = parent.childNodes[i];
  if( current_child.nodeType == 3 )
    parent.removeChild( current_child );
}
筱武穆 2024-11-08 06:14:30

我修改了尼尔斯的答案,使其适用于多个元素:

$( elements ).each(function() {  
var child = $(this).children();  
$(this).html(child);   
});

I modified Neals answer so it works for multiple elements:

$( elements ).each(function() {  
var child = $(this).children();  
$(this).html(child);   
});
相思故 2024-11-08 06:14:30

非常简单,只需使用此代码:

jQuery(function($){

    // Replace 'td' with your html tag
    $("td").html(function() { 

    // Replace 'ok' with string you want to change, you can delete 'hello everyone' to remove the text
          return $(this).html().replace("ok", "hello everyone");  

    });
});

这是完整的示例: https://blog.hfarazm .com/remove-unwrapped-text-jquery/

Very simple just use this code:

jQuery(function($){

    // Replace 'td' with your html tag
    $("td").html(function() { 

    // Replace 'ok' with string you want to change, you can delete 'hello everyone' to remove the text
          return $(this).html().replace("ok", "hello everyone");  

    });
});

Here is full example: https://blog.hfarazm.com/remove-unwrapped-text-jquery/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文