简体   繁体   English

使用JavaScript将新的HTML标签添加到文本块

[英]Adding new HTML tags with JavaScript to a text block

After page is done loading, I would like script to add tags to certain parts of the text within it. 页面加载完成后,我希望脚本将标签添加到其中的文本的某些部分。 For example: 例如:

    <p> Some text is here. The page has already finished loading</p>

to

    <p> Some text is <b class="bolded">here</b>. The page has already finished loading</p>

You can do this by calling a function onload of page. 您可以通过调用页面的onload函数来实现。 Give the ID to the elements of HTML and write the below code 将ID赋予HTML元素,并编写以下代码

HTML : HTML:

<p  id='testing1'> Some text is here. The page has already finished loading</p>

Javascript: Javascript:

 function onLoad()
  {
     var strTest = $("#testing1").html();
strTest = strTest.replace("here",'<b class="bolded" style="font-weight:bold;">here</b>');
console.log('strTest:',strTest);
  }

Maybe u can do something like $("p") or add an ID at your <p> example: 也许您可以执行类似$(“ p”)的操作,或者在您的<p>示例中添加一个ID:

<p  id='testing1'> Some text is here. The page has already finished loading</p>

then when after the string render and you may use the script as below to replace it: 然后在字符串渲染之后,您可以使用以下脚本替换它:

var strTest = $("#testing1").html();
strTest = strTest.replace("here",'<b class="bolded" style="font-weight:bold;">here</b>');
console.log('strTest:',strTest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM