Javascript to untag spans without id [英] Javascript to untag spans without id

查看:53
本文介绍了Javascript to untag spans without id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在字符串中查找和取消没有id的跨度?我的文本有一堆跨度,其中一些有ID,有些则没有。

Would it be possible to find and untag spans that do not have ids within a string? I have a text that has bunch of spans some of which have ids and others don't.

输入:

<span>Hi there!</span><span id="blabla">This is a test</span>

输出:

Hi there!<span id="blabla">This is a test</span>

我更喜欢JavaScript函数但我不介意使用jQuery,如果它让事情变得更容易!

I prefer JavaScript functions but I wouldn't mind using jQuery if it makes things easier!

推荐答案

您应该可以使用 :不是 伪选择器,以及 has-attribute selector:

You should be able to use a combination of the :not pseudo-selector, and a "has-attribute" selector:

$("span:not([id])").contents().unwrap();

这是工作示例。请注意HTML代码是如何由4个 span 元素组成的,CSS规则适用于所有 span 元素,但是不适用于没有 id 的2 span 元素,因为它们已被上述jQuery解包。

Here's a working example. Notice how the HTML code is made up of 4 span elements, the CSS rule applies to all span elements, but does not apply to the 2 span elements without an id, because they have been unwrapped by the above jQuery.

内容 方法返回所选元素的所有子元素,并 unwrap 删除父母,在这种情况下将是不需要的 span

这篇关于Javascript to untag spans without id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆