jQuery - 如果类中有特定单词,则将类转移到另一个元素?
我有这个问题,我想将特定的(但不是特定的)类从元素转移到另一个元素。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
这个可能存在与处理顺序相关的问题,因此选择器需要一个跨度位于另一个跨度内。
Try this:
This may have problems relating to order of processing, hence why the selector requires a span inside another span.
我现在无法测试它,但稍后我会发布一个jsFiddle。应该对正则表达式进行处理,我只是想给您一个概述。基线是 的用法
.removeClass()
带有函数和 属性包含前缀选择器[名称|=“值”]。更新:修复了代码并添加了演示。该代码获取
span
上以THIS-
开头的所有类名,并将它们移动到其父级(您可以在wrap()
或摆脱我的选择器并使用你的,例如$(myselector).wrap(mystuff).removeClass(...);
)。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-
onspan
s and moves them to their parent (you can run it afterwrap()
or get rid of my selector and use yours like$(myselector).wrap(mystuff).removeClass(...);
).