[英]replace href link text
Is my code correct? 我的代码是否正确?
function 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 我试图删除像<a href ="#">oldtexts</a>
这样a href
标签之间的字符串“oldtexts”,但我仍然看到oldtexts并且没有任何内容被替换
I didn't try this one, but this may work. 我没试过这个,但这可能有用。 (uses jQuery) (使用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: 你有标记jQuery,所以我建议使用它:
$(function(){
$('a').each(function(){
var $this = $(this);
$this.html($this.html().replace(/oldtext/g, ''));
});
});
Use Jquery :) 使用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');
});
});
如果您尝试使用/\\b(oldtexts)\\b/g
或jQuery .replace
什么?
Since you seem to be using jQuery, the cleanest solution seems to be using a function for .html()
. 由于您似乎使用jQuery,最干净的解决方案似乎是使用.html()
函数 。
$('a').html(function (index, oldhtml) {
return oldhtml.replace(/oldtexts/g, '');
});
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);
然后你可以在任何jQuery对象上调用它: $('a').html(replace_stuff);
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.