I want to remove the text "By:" from an element in my website. I want the rest of the text to remain there. How can I achieve that with jQuery? Thank you.
我想从我网站上的一个元素中删除“By:”文本。我希望文本的其余部分保留在那里。我怎样才能用jQuery实现这一目标?谢谢。
The HTML
HTML
<div id="text">By: Anonymous From Minnesota</div>
I want it to just be:
我希望它只是:
<div id="text">Anonymous From Minnesota</div>
4 个解决方案
#1
31
If you wanted to find every element with "By:" in it and remove it, you could try this:
如果你想在其中找到包含“By:”的每个元素并将其删除,你可以试试这个:
$(':contains("By:")').each(function(){
$(this).html($(this).html().split("By:").join(""));
});
This removes any occurrence of "By:" in any element. If you want to target specfic type of elements, simply change $(':contains("By:")')
to include whatever selector suits you.
这将删除任何元素中出现的“By:”。如果你想要定义特定类型的元素,只需更改$(':contains(“By:”)')以包含适合你的选择器。
#2
10
a more global solution using regular expressions. This will replace By:
even if your string structure will change.
使用正则表达式的更全局的解决方案这将替换为:即使您的字符串结构将发生变化。
var str = $('div').text().replace(/By:/g, '');
$('div').text(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="text">By: Anonymous From Minnesota</div>
#3
3
var str = $("#text").text();
$("#text").text(str.substring(3));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="text">By: Anonymous From Minnesota</div>
Using javascript's substring
and give your div an ID to access it easily:
使用javascript的子字符串并为您的div提供ID以便轻松访问它:
html:
HTML:
<div id="text">By: Anonymous From Minnesota</div>
jquery:
jQuery的:
var str = $("#text").text();
$("#text").text(str.substring(3));
You could do it in one line too:
你也可以在一行中完成它:
$("#text").text($("#text").text().substring(3));
#4
1
give an unique id to div, t:
给div一个唯一的id,t:
$('#div_id').html($('#div_id').html().substring(3));
fiddle : http://jsfiddle.net/ChUB4/
小提琴:http://jsfiddle.net/ChUB4/
#1
31
If you wanted to find every element with "By:" in it and remove it, you could try this:
如果你想在其中找到包含“By:”的每个元素并将其删除,你可以试试这个:
$(':contains("By:")').each(function(){
$(this).html($(this).html().split("By:").join(""));
});
This removes any occurrence of "By:" in any element. If you want to target specfic type of elements, simply change $(':contains("By:")')
to include whatever selector suits you.
这将删除任何元素中出现的“By:”。如果你想要定义特定类型的元素,只需更改$(':contains(“By:”)')以包含适合你的选择器。
#2
10
a more global solution using regular expressions. This will replace By:
even if your string structure will change.
使用正则表达式的更全局的解决方案这将替换为:即使您的字符串结构将发生变化。
var str = $('div').text().replace(/By:/g, '');
$('div').text(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="text">By: Anonymous From Minnesota</div>
#3
3
var str = $("#text").text();
$("#text").text(str.substring(3));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="text">By: Anonymous From Minnesota</div>
Using javascript's substring
and give your div an ID to access it easily:
使用javascript的子字符串并为您的div提供ID以便轻松访问它:
html:
HTML:
<div id="text">By: Anonymous From Minnesota</div>
jquery:
jQuery的:
var str = $("#text").text();
$("#text").text(str.substring(3));
You could do it in one line too:
你也可以在一行中完成它:
$("#text").text($("#text").text().substring(3));
#4
1
give an unique id to div, t:
给div一个唯一的id,t:
$('#div_id').html($('#div_id').html().substring(3));
fiddle : http://jsfiddle.net/ChUB4/
小提琴:http://jsfiddle.net/ChUB4/