简体   繁体   English

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

[英]jQuery - Transfer class to another element if the class has specific word in it?

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 ) 基本的想法是,如果你在具有特定单词“THIS”的内部跨度中编写一个类,它将被转移到外部跨度 (使用.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. 带有“THIS”的课程是一个类似于... 20种不同的变化(这个东西,这个 - 东西 - 任何东西,这个和那......等等......)和“anotherClass”完全是随机的类并且可能会有多个......这取决于我想要插入的数量。

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... 我只能这样做所有类都被复制到外跨度然后我会从内部跨度删除所有类..但这有点失败所有目的,因为它不会留下“anotherClass”,它将其复制到外部元素。 ..

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. 可能存在与处理顺序有关的问题,因此选择器需要跨越另一个跨度的范围。

$('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. 我现在无法测试它,但后来我会发布一个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"] . 基线是带有函数的.removeClass()属性包含前缀选择器[name | =“value”]的用法。

UPDATE: fixed the code and added Demo . 更新:修复代码并添加Demo The code grabs all the classnames starting with THIS- on span s 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(...); ). 代码抓住开头的所有类名THIS-span S和它们移动到它们的父(后就可以运行它wrap()或摆脱我的选择和使用你像$(myselector).wrap(mystuff).removeClass(...); )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM