admin管理员组文章数量:1315960
I am using latest Jquery and the following script:
<script type="text/javascript">
$(document).ready(function(){
var el = $('.title');
el.html(el.html().replace(/\./gi, " "));
});
</script>
This is the div:
<div class="title">The.quick.brown.fox</div>
What it does is replaces all dots with spaces in a DIV with class "title" and really it works fine for this job.
But I have many DIVs with the same "title" class with different strings in them and i need them all to have dots replaced with spaces. But here the problem appears as all what i get is the same result string on all these DIVs "The quick brown fox" while all result strings should be different as all source strings are different...
What do i do to get dots replaced in all DIVs with class "title" and all different strings in each DIV?
Thanks
I am using latest Jquery and the following script:
<script type="text/javascript">
$(document).ready(function(){
var el = $('.title');
el.html(el.html().replace(/\./gi, " "));
});
</script>
This is the div:
<div class="title">The.quick.brown.fox</div>
What it does is replaces all dots with spaces in a DIV with class "title" and really it works fine for this job.
But I have many DIVs with the same "title" class with different strings in them and i need them all to have dots replaced with spaces. But here the problem appears as all what i get is the same result string on all these DIVs "The quick brown fox" while all result strings should be different as all source strings are different...
What do i do to get dots replaced in all DIVs with class "title" and all different strings in each DIV?
Thanks
Share Improve this question asked Jan 6, 2012 at 9:35 CamSpyCamSpy 4012 gold badges15 silver badges26 bronze badges3 Answers
Reset to default 5You can use each() to iterate over the matched elements, or pass a function to html() and pute the new text there:
$(document).ready(function() {
$(".title").html(function(index, currentHtml) {
return currentHtml.replace(/\./gi, " ");
});
});
Just use jQuery each
method to iterate over all elements with class .title
:
$(document).ready(function(){
$('.title').each(function(){
$(this).html($(this).html().replace(/\./gi, " "));
});
});
As long as you have only text inside your divs, suggested functions will work just fine. However, to allow arbitrary html (like The.quick.brown <img src='fox.jpg'>
) the code should be more accurate.
$('.title').each(function() {
if (this.nodeType == 3)
this.nodeValue = this.nodeValue.replace(/\./g, " ");
else
$(this).contents().each(arguments.callee);
});
Basically, you recursively iterate over all descendants of a node and replace only nodes of type 3 (=text).
Fiddle: http://jsfiddle/jZgUY/
本文标签: javascriptJquery replace dots with spaces inside of all DIVs with one classStack Overflow
版权声明:本文标题:javascript - Jquery replace dots with spaces inside of all DIVs with one class - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741996257a2410106.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论