最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery - change content of html <b> tag - Stack Overflow

programmeradmin1浏览0评论

I have set of frames and html pages . I have selected one of the frames html element from another html page and I want to change the content using jquery.

Here , I want to change "test" to "something" . Please let me know , how to achieve this.

HTML element in frame 0 :

<span class="topmid-left">Connected to <b>My Site 'test' </b> </span>

The same element can be selectable by jquery by setting context :

**query -**  $("b",window.frames[0].document)

**result -** <b>​My Site 'test' ​</b>​

How to change the content of tag here.

Thanks.

I have set of frames and html pages . I have selected one of the frames html element from another html page and I want to change the content using jquery.

Here , I want to change "test." to "something." . Please let me know , how to achieve this.

HTML element in frame 0 :

<span class="topmid-left">Connected to <b>My Site 'test.' </b> </span>

The same element can be selectable by jquery by setting context :

**query -**  $("b",window.frames[0].document)

**result -** <b>​My Site 'test.' ​</b>​

How to change the content of tag here.

Thanks.

Share Improve this question asked Apr 7, 2016 at 6:34 JavaUserJavaUser 26.4k47 gold badges116 silver badges144 bronze badges 5
  • 1 $("b").text(YOUR_TEXT) ? or $("span.topmid-left b").text(YOUR_TEXT) – Rayon Commented Apr 7, 2016 at 6:35
  • Uncaught TypeError: $(...).text is not a function(…) – JavaUser Commented Apr 7, 2016 at 6:38
  • 1 Have you included jQuery ? – Rayon Commented Apr 7, 2016 at 6:39
  • Yes . Thats why I am able to select the text. – JavaUser Commented Apr 7, 2016 at 6:39
  • Can you share executable demo/snippet or JSFiddle ? – Rayon Commented Apr 7, 2016 at 6:40
Add a ment  | 

3 Answers 3

Reset to default 4

Here is the fiddle: https://jsfiddle/swaprks/L1rq4o7a/

Use jquery and add the following to your javascript:

$(".topmid-left b").html("My Site 'something.'");

try this:

<script>
var str = 'your text';
$( "b" ).html( str );
or 
$("b").text(YOUR_TEXT)
</script>

Learn More

Try this it will work :

Html :

<span class="topmid-left">Connected to <b>My Site 'test.'</b> </span>

Script :

// return the inner text of <b>.
var boldText = $("span.topmid-left b").text(); 

// returns the array of length 3.
var splitArr = boldText.split('\''); 

// returns test.
var text = splitArr[1];

// replace the text. with abc(or any text you want).
var repText = boldText.replace(text, "xyz"); 

// [Output] My Site 'xyz'
console.log(repText);

// assign new text into html <b> tag.
$("span.topmid-left b").text(repText);

Working Jsfiddle : https://jsfiddle/LxcLb012/2/

发布评论

评论列表(0)

  1. 暂无评论