使用JavaScript将元素中的某些文本转换为元素本身
使用JavaScript、JQuery或其他任何东西都可以做到这一点吗 假设我有一个这样的HTML文件使用JavaScript将元素中的某些文本转换为元素本身,javascript,html,Javascript,Html,使用JavaScript、JQuery或其他任何东西都可以做到这一点吗 假设我有一个这样的HTML文件 <div> <p>Hello World</p> </div> 你好,世界 我想把“World”本身变成一个span元素,就像这样(这样我就可以设计“World”) 你好,世界 因为您的问题中有很多未知项,所以我假设您已经知道要在其周围添加html标记的字符串/单词 因此,牢记这一点,以下解决方案应该有效: HTML: <d
<div>
<p>Hello World</p>
</div>
你好,世界
我想把“World”本身变成一个span元素,就像这样(这样我就可以设计“World”)
你好,世界
因为您的问题中有很多未知项,所以我假设您已经知道要在其周围添加html标记的字符串/单词
因此,牢记这一点,以下解决方案应该有效:
HTML:
<div>
<p id="my-text">Hello World, Again!</p>
</div>
世界你好,又来了
JavaScript:
const stringToBeReplaced = "World"; // what you want to replace
const innerText = document.getElementById("my-text").innerText; //grab the text
const beginIndex = innerText.indexOf(stringToBeReplaced); // get text where string begins
// if string exists
if (beginIndex >= 0) {
const textWithTag =
"<span style='color: red'>" + stringToBeReplaced + "</span>";
const newString = innerText.replace(stringToBeReplaced, textWithTag);
// replace the text with new string
document.getElementById("my-text").innerHTML = newString;
}
const stringtobereplace=“World”//你想替换什么
const innerText=document.getElementById(“我的文本”).innerText//抓取文本
const beginIndex=innerText.indexOf(stringToBeReplaced);//获取字符串开始处的文本
//如果字符串存在
如果(beginIndex>=0){
const textWithTag=
“+stringToBeReplaced+”;
const newString=innerText.replace(StringToBeReplace,textWithTag);
//用新字符串替换文本
document.getElementById(“我的文本”).innerHTML=newString;
}
希望这是您一直在询问和寻找的内容。因为您的问题中有很多未知数,所以我假设您已经知道要在其周围添加html标记的字符串/单词 因此,牢记这一点,以下解决方案应该有效: HTML:
<div>
<p id="my-text">Hello World, Again!</p>
</div>
世界你好,又来了
JavaScript:
const stringToBeReplaced = "World"; // what you want to replace
const innerText = document.getElementById("my-text").innerText; //grab the text
const beginIndex = innerText.indexOf(stringToBeReplaced); // get text where string begins
// if string exists
if (beginIndex >= 0) {
const textWithTag =
"<span style='color: red'>" + stringToBeReplaced + "</span>";
const newString = innerText.replace(stringToBeReplaced, textWithTag);
// replace the text with new string
document.getElementById("my-text").innerHTML = newString;
}
const stringtobereplace=“World”//你想替换什么
const innerText=document.getElementById(“我的文本”).innerText//抓取文本
const beginIndex=innerText.indexOf(stringToBeReplaced);//获取字符串开始处的文本
//如果字符串存在
如果(beginIndex>=0){
const textWithTag=
“+stringToBeReplaced+”;
const newString=innerText.replace(StringToBeReplace,textWithTag);
//用新字符串替换文本
document.getElementById(“我的文本”).innerHTML=newString;
}
希望这就是你所要求和寻找的。
str.replace解决了这个问题。@Umer Hassan的评论是正确的。
str.replace解决了这个问题。@Umer Hassan的评论是正确的。您是否总是知道要在其周围添加span标记的特定文本?我已经添加了答案,假设您已经知道了这个词,请接受答案,如果这是您想要的,谢谢!您是否总是知道要在其周围添加span标记的特定文本?我已添加了答案,假设您已经知道该单词,请接受答案,如果您正在寻找答案,谢谢!伙计,我差一点就到了。我尝试用使用createElement()创建的元素替换StringToBeReplace。。。我本来可以说得更清楚的,对不起。谢谢你的帮助!是的,这也可以,但我只是想在任何地方重复使用
stringToBeReplaced
,可以随意修改。不用担心,我知道我们都已经到了死胡同,快乐编码!:)伙计,我差一点就到了。我尝试用使用createElement()创建的元素替换StringToBeReplace。。。我本来可以说得更清楚的,对不起。谢谢你的帮助!是的,这也可以,但我只是想在任何地方重复使用stringToBeReplaced
,可以随意修改。不用担心,我知道我们都已经到了死胡同,快乐编码!:)