jQuery - 如果类中有特定单词,则将类转移到另一个元素?

发布于 2024-11-02 10:10:48 字数 696 浏览 8 评论 0原文

我有这个问题,我想将特定的(但不是特定的)类从元素转移到另一个元素。

this:

<span>
   <span class="THIS-something-something anotherClass"></span>
</span>

into:

<span class="THIS-something-something">
   <span class="anotherClass"></span>
</span>

基本思想是,如果您在内部跨度中编写一个具有特定单词“THIS”的类,它将被转移到外部跨度(外部跨度是用 .wrap 生成的)

带有“THIS”的类是一个具有大约20种不同变体的类(THIS-something-something、THIS-something-anything、THIS-and-that ..等等..),而“anotherClass”是完全随机的类,也可能有多个......取决于我想插入多少。

我不知道这会如何发展。我可能认为这一切都是错误的吗?

我只能将所有类复制到外部跨度,然后从内部跨度删除所有类..但这有点破坏了所有目的,因为它不会留下“anotherClass”并将其复制到外部元素。 ..

I have this problem that i want to transfer specific ( yet not that specific ) classes from element to another.

this:

<span>
   <span class="THIS-something-something anotherClass"></span>
</span>

into:

<span class="THIS-something-something">
   <span class="anotherClass"></span>
</span>

Basic idea is that if you write a class in that inner span that has specific word "THIS" it would get transfered to the outer span ( the outer span is generated there with .wrap )

class with "THIS" is a class that has like.. 20 different variations ( THIS-something-something, THIS-something-anything, THIS-and-that ..and so on.. ) and "anotherClass" is totally random class and there would possibly be multiple of those as well..depending on how many i would like to insert.

I have no idea how that would go down.. Am i maybe thinking about this all wrong?

I can only make it so that all classes get copied to the outer span and then i would remove all classes from the inner span.. but that kinda defeats all purposes as it doesnt leave "anotherClass" and it copies that to the outer element...

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

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

发布评论

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

评论(2

清风无影 2024-11-09 10:10:48

试试这个:

$('span > span').each(function() {
    var all = $(this).attr('class').split(' ');
    for (var i = 0; i < all.length; ++i) {
        var cls = all[i];
        if (cls.indexOf('THIS-') == 0) {
            $(this).parent().addClass(cls);
            $(this).removeClass(cls);
        }
    }
});

这个可能存在与处理顺序相关的问题,因此选择器需要一个跨度位于另一个跨度内。

Try this:

$('span > span').each(function() {
    var all = $(this).attr('class').split(' ');
    for (var i = 0; i < all.length; ++i) {
        var cls = all[i];
        if (cls.indexOf('THIS-') == 0) {
            $(this).parent().addClass(cls);
            $(this).removeClass(cls);
        }
    }
});

This may have problems relating to order of processing, hence why the selector requires a span inside another span.

铜锣湾横着走 2024-11-09 10:10:48
$('span[class|="THIS"]').removeClass(function (index, class) {
    var thisclass=class.match(/THIS-[^\s]+/g).join(' ');
    $(this).parent().addClass(thisclass);
    return thisclass;
});

我现在无法测试它,但稍后我会发布一个jsFiddle。应该对正则表达式进行处理,我只是想给您一个概述。

基线是 的用法.removeClass() 带有函数和 属性包含前缀选择器[名称|=“值”]

更新:修复了代码并添加了演示。该代码获取 span 上以 THIS- 开头的所有类名,并将它们移动到其父级(您可以在 wrap() 或摆脱我的选择器并使用你的,例如 $(myselector).wrap(mystuff).removeClass(...);)。

$('span[class|="THIS"]').removeClass(function (index, class) {
    var thisclass=class.match(/THIS-[^\s]+/g).join(' ');
    $(this).parent().addClass(thisclass);
    return thisclass;
});

I can't test it right now, but later I will post a jsFiddle. The regex should be worked on, I just wanted to give you an outline.

The baseline is the usage of .removeClass() with a function and the Attribute Contains Prefix Selector [name|="value"].

UPDATE: fixed the code and added Demo. The code grabs all the classnames starting with THIS- on spans and moves them to their parent (you can run it after wrap() or get rid of my selector and use yours like $(myselector).wrap(mystuff).removeClass(...);).

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