[英]Adding new HTML tags with JavaScript to a text block
页面加载完成后,我希望脚本将标签添加到其中的文本的某些部分。 例如:
<p> Some text is here. The page has already finished loading</p>
至
<p> Some text is <b class="bolded">here</b>. The page has already finished loading</p>
您可以通过调用页面的onload函数来实现。 将ID赋予HTML元素,并编写以下代码
HTML:
<p id='testing1'> Some text is here. The page has already finished loading</p>
Javascript:
function onLoad()
{
var strTest = $("#testing1").html();
strTest = strTest.replace("here",'<b class="bolded" style="font-weight:bold;">here</b>');
console.log('strTest:',strTest);
}
也许您可以执行类似$(“ p”)的操作,或者在您的<p>
示例中添加一个ID:
<p id='testing1'> Some text is here. The page has already finished loading</p>
然后在字符串渲染之后,您可以使用以下脚本替换它:
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.