replace href link text
Is my code correct?
fu开发者_开发问答nction replace_stuff() {
document.body.innerHTML = document.body.innerHTML.replace(/oldtexts/g,'');
}
I am trying to remove the string "oldtexts" between an a href
tag like
this <a href ="#">oldtexts</a>
but i still see oldtexts and nothing gets replaced
I didn't try this one, but this may work. (uses jQuery)
$('a[href]').each(function(i){
if($(this).html()=="oldtext") $(this).html(" ");
});
or
$('a[href]').each(function(i){
$(this).html($(this).html().replace(/regex/g,"blahblah"));//warning : if replaced text contains nothing(""), then this will not work.
});
You have tagged jQuery, so I would suggest to use it:
$(function(){
$('a').each(function(){
var $this = $(this);
$this.html($this.html().replace(/oldtext/g, ''));
});
});
Use Jquery :)
$(document).ready(function(){
$('#idofatag').text('replacevalueofoldtexts');
});
if you want to trigger this change on a click of a button,
$(document).ready(function(){
$('#someButtonid').click(function(){
$('#idofatag').text('replacevalueofoldtexts');
});
});
http://jquery.com/
Since you seem to be using jQuery, the cleanest solution seems to be using a function for .html()
.
$('a').html(function (index, oldhtml) {
return oldhtml.replace(/oldtexts/g, '');
});
jsFiddle Demo
If you want to write a reusable function, you can do that of course:
function replace_stuff(index, oldhtml) {
return oldhtml.replace(/oldtexts/g, '');
}
And then you can invoke it on any jQuery object: $('a').html(replace_stuff);
.
What happens if you try using /\b(oldtexts)\b/g
or the jQuery .replace
?
精彩评论