开发者

replace a text with another within a tag

开发者 https://www.devze.com 2023-01-19 09:25 出处:网络
I\'m sure it\'s stupidly easy, but how can I replace just the text within an html tag. For instance, from this:

I'm sure it's stupidly easy, but how can I replace just the text within an html tag. For instance, from this:

<div style="display: block;">blah blah</div>
<a h开发者_StackOverflowref="#" style="display: inline; ">(Old text)</a>

to this:

<div style="display: block;">blah blah</div>
<a href="#" style="display: inline; ">(New text)</a>

replace Old text => New text

Many thanks in advance.


Just select the element and change its contents

$("a").html("(New Text)");

References

  • html
  • text
  • jQuery selectors


Put an id on the element containing the text:

<div style="display: block;">blah blah</div><a href="#" style="display: inline; " id="replaceme">(Old text)</a>

And use

$('#replaceme').html('New text');

This is the basic, I think you can work it out from here :)


First, give your element an ID so you can refer to it:

<a id="something" href="#" style="display: inline; ">(Old text)</a>

Then use the string replace function:

var str = $('#something').text();
$('#something').text(str.replace('Old text', 'New text'));
0

精彩评论

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