在许多 div 中,我必须通过用句点替换空格来更改类名....
所以我试了一下
$(jqueryElements).each(function(index)
{
jqueryElements[index].className.replace(' ','.');
});
当类(class)有两个单词时它工作正常......但是当类(class)名称有 3 个或更多单词时它失败......
className='one word';
jqueryElement[index].className.replace(' ','.'); // console-> one.word
className='many many words';
jqueryElement[index].className.replace(' ','.'); // console-> many.many words
有什么问题吗??
我正在使用 Chrome 25、Win7、jQuery 1.8
编辑 2
我需要替换空格以搜索具有特定类名的所有跨度元素。
所以我就这样使用jquery...
$('#span-container').find('span.'+jqueryElements[index].className.replace(' ','.').text('there are '+span_counter+'spans with this classname');
这个请求的结果应该是:
$('#span-container').find('span.many.many.words).text('there are '+span_counter+'spans with this classname');
相反,我有:
$('#span-container').find('span.many.many words).text('there are '+span_counter+'spans with this classname');
最佳答案
不要使用替换,使用split
和join
:
jqueryElements[index].className.split(' ').join('.');
关于javascript - 如何使用 String.replace() 替换所有出现的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15125989/