I need to rem开发者_C百科ove the word 'Share' that is generated from a div (and replace it with nothing) using jQuery or JS. For example
Share It isn’t confirmed yet, but VatorNews is reporting that Google as tipped over the last couple of weeks has [...]Can someone please help?
Steps:
- Get the value of the
$(#div_id)
, usingtext()
and then store it in a variable. - Use regular expressions and string functions to remove it from the text, if present there.
- Using jquery or javascript, again use the
text( textString )
function to set the value again, if you want to or use it as you wish.
For info on text()
, refer here
Try with this:
$("#divId").text($("#divId").text().replace("Share", ""));
I have put the text into a variable called txt. How do I now search for the string 'Share'?
You can;
txt = txt.replace(/\bshare\b/ig, "");
\b
to force a match on word boundary, ig
for Ignore case, Global (replace all)
Thanks to everyone who helped with this. Much appreciated. To clarify what I needed: I had 4 divs with unique generated content. The string 'Share' always appeared at the beginning of a span tag within the content. I needed to remove that string. This is the code that solved it:
//Removes 'Share' string from featured posts excerpts on home page
jQuery('span.excerpt').each(function() {
var share = jQuery(this);
share.text(share.text().substr(6) );
});
精彩评论