嵌套CSS类的JavaScript正则表达式 [英] Javascript Regular Expression for nested CSS classes
问题描述
按照我的问题在这里,我一直在试图写一个类似的位在Javascript中的代码,将检测从嵌套的CSS类中发生在文本中的Unicode字符范围。到目前为止,我能够使它的工作在DIV类,例如:
$(document).ready ){
$('h2.ClassName')。each(function(index,DOMElement){
if(/ [\\\-\\\] + /。test($(DOMElement))。 text())){
$(this).removeClass('ClassName')。addClass('CSS-ClassName');
}
})
}但是,类似的Javascript在以下类中不起作用:
< ul>
< h4 class =ClassName>文字< / h4>
< li class =ClassName>文字< / li>
< ul>任何想法?
尊敬,
我。
解决方案如果你想让jQuery找到使用相同 ClassName
的其他元素,那么你需要将它们添加到你的选择器, >
$(document).ready(function(){
$('h2.ClassName,h4.ClassName,li.ClassName ').each(function(index,DOMElement){
if(/ [\\\-\\\] + /。test($(DOMElement).text())){
$ this).removeClass('ClassName')。addClass('CSS-ClassName');
}
})
});
如果你想得到真正的通用,并改变所有元素 ClassName
,那么您可以将选择器更改为:
/ * ... * /
('.ClassName')。 / * ...你的想法... * /
/ * ... * /
$ b b
希望有帮助。
following my question here, I have been trying to write a similar bit of code in Javascript that would detect a range of Unicode characters occurring in the text from nested CSS classes. So far, I was able to make it work on DIV Classes, for instance:
$(document).ready(function() {
$('h2.ClassName').each(function(index, DOMElement){
if(/[\uE000-\uF8FF]+/.test($(DOMElement).text())) {
$(this).removeClass('ClassName').addClass('CSS-ClassName');
}
})
});
However, a similar Javascript does not work on the following classes:
<ul>
<h4 class="ClassName">Text</h4>
<li class="ClassName">Text</li>
<ul>
Any idea?
Thanks indeed,
Regards,
I.
解决方案 If you want jQuery to find other elements using the same ClassName
then you need to add them to your selector, like so:
$(document).ready(function() {
$('h2.ClassName, h4.ClassName, li.ClassName').each(function(index, DOMElement){
if(/[\uE000-\uF8FF]+/.test($(DOMElement).text())) {
$(this).removeClass('ClassName').addClass('CSS-ClassName');
}
})
});
If you want to get really generic, and change all elements with ClassName
then you would change the selector to:
/* ... */
$('.ClassName').each(); /* ... you get the idea ... */
/* ... */
Hope that helps.
这篇关于嵌套CSS类的JavaScript正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!