开发者

Replacing a string inside a a tag with a different string of text

开发者 https://www.devze.com 2023-01-28 23:07 出处:网络
I know this should be simple but I can\'t make it work. I have some text inside antag which I need to change. It\'s rendered by my CMS so 开发者_运维技巧I can\'t change it on the back end. Here is the

I know this should be simple but I can't make it work. I have some text inside an tag which I need to change. It's rendered by my CMS so 开发者_运维技巧I can't change it on the back end. Here is the html.

<a href="/OrderRetrievev2.aspx?CatalogueID=105038" class="cartSummaryLink">View Cart</a>

I need to change "View Cart" to "View Summary". Here is what I was trying to use with my jQuery.

    $'a.cartSummaryLink').text().replace("View Cart" , "View Summary");

It's not working. Thanks in advance for any help our suggestions!


I'm not sure if it was just a copy-pase error, but you need to have an opening ( on your $() function. Otherwise you will get a syntax error. Then, there is no need to call .replace(), .text() is a both a getter and a setter, so you can just pass "View Summary" to .text(), and it will change the element's text.

Like so:

$('a.cartSummaryLink').text("View Summary");


.text() returns the text. .text(val) replaces the text.

$('a.cartSummaryLink').text("View Summary");


For future reference, let's say there was other text in the link, but you only want to replace View Cart to become View Summary. You can try something like this..

var link = $('a.cartSummaryLink');
var linkText = link.text();
link.text( linkText.replace("View Cart", "View Summary") );

This makes use of the .text() function from jQuery, but the String manipulation function available from Javascript.

Hope it helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消