Javascript 用图像自动替换某些文本

Javascript 用图像自动替换某些文本,javascript,html,jquery,Javascript,Html,Jquery,我目前正在创建一个小网站,我想不必按下按钮就可以用图像自动替换某些文本。我想让它自动完成,但我似乎无法让它工作。谢谢大家! 这是我的密码: html http://localhost:1111/ javascript: <script> var str = document.getElementById("test").innerHTML; var res = str.replace("http://localhost:1111", Ele

我目前正在创建一个小网站,我想不必按下按钮就可以用图像自动替换某些文本。我想让它自动完成,但我似乎无法让它工作。谢谢大家!

这是我的密码:

html

http://localhost:1111/

javascript:

<script>
var str = document.getElementById("test").innerHTML; 
var res = str.replace("http://localhost:1111", Element.innerHTML = "<img 
src='https://www.stockgumshoe.com/wp-content/uploads/2014/06/test.jpg");
document.getElementById("demo").innerHTML = res;
</script>

var str=document.getElementById(“test”).innerHTML;
变量res=str.replace(“http://localhost:1111,Element.innerHTML=
而不是这个:

<script>
var str = document.getElementById("test").innerHTML; 
var res = str.replace("http://localhost:1111", lement.innerHTML = "<img 
src='https://www.stockgumshoe.com/wp-content/uploads/2014/06/test.jpg");
document.getElementById("demo").innerHTML = res;
</script>

var str=document.getElementById(“test”).innerHTML;
变量res=str.replace(“http://localhost:1111“,lement.innerHTML=”
试试这个:

<script>
document.getElementById("test").innerHTML = `<img 
src="https://www.stockgumshoe.com/wp-content/uploads/2014/06/test.jpg" />`;
</script>

document.getElementById(“test”).innerHTML=`;
而不是这个:

<script>
var str = document.getElementById("test").innerHTML; 
var res = str.replace("http://localhost:1111", lement.innerHTML = "<img 
src='https://www.stockgumshoe.com/wp-content/uploads/2014/06/test.jpg");
document.getElementById("demo").innerHTML = res;
</script>

var str=document.getElementById(“test”).innerHTML;
变量res=str.replace(“http://localhost:1111“,lement.innerHTML=”
试试这个:

<script>
document.getElementById("test").innerHTML = `<img 
src="https://www.stockgumshoe.com/wp-content/uploads/2014/06/test.jpg" />`;
</script>

document.getElementById(“test”).innerHTML=`;

对于此代码,您不需要任何
id
class

const paragraphs = document.querySelectorAll('p')

paragraphs.forEach((paragraph) => {
  if (paragraph.innerText == 'http://localhost:1111/') {
    return (paragraph.innerHTML = `
    <img src='https://www.stockgumshoe.com/wp-content/uploads/2014/06/test.jpg' />
    `)
  }
})

const段落=document.querySelectorAll('p'))
段落。forEach((段落)=>{
如果(段落.innerText=='http://localhost:1111/') {
返回(段落.innerHTML=`
`)
}
})

对于此代码,您不需要任何
id
class

const paragraphs = document.querySelectorAll('p')

paragraphs.forEach((paragraph) => {
  if (paragraph.innerText == 'http://localhost:1111/') {
    return (paragraph.innerHTML = `
    <img src='https://www.stockgumshoe.com/wp-content/uploads/2014/06/test.jpg' />
    `)
  }
})

const段落=document.querySelectorAll('p'))
段落。forEach((段落)=>{
如果(段落.innerText=='http://localhost:1111/') {
返回(段落.innerHTML=`
`)
}
})

它可以工作,我可以看到图像,但我不能在项目中使用类或id。你是否可以通过查找文本,不使用javascript查找id或类来完成这项工作?它可以工作,我可以看到图像,但我不能在项目中使用类或id。你是否可以通过查找文本,不使用javascript查找id或类来完成这项工作ext?您是否打算使用两个不同的元素ID(
test
demo
),或者你是想使用相同的元素ID,所以你实际上是在进行替换吗?哦,我一定是把演示ID放错了。我以前测试过按钮是否可以使用。但我决定不按按钮自动执行此操作。你打算使用两个不同的元素ID吗(
测试
演示
),或者你是想使用相同的元素ID,所以你实际上是在进行替换吗?哦,我一定是把演示ID放错了。我以前测试过按钮是否可以使用。但我决定不按按钮自动执行此操作。你能试一下编辑过的代码吗?至于使用文本定位元素,它是可能。
var元素=$(“p:contains('http://localhost:1111“)”
将为您提供包含“”“您好,现在可以了,但是..我可以在没有ID或类的情况下执行此操作吗?意思是我可以用javascript查找文本并替换它吗?您可以尝试编辑的代码吗?就使用文本查找元素而言,这是可能的。
var element=$('p:contains('s')”http://localhost:1111“)”
将为您提供包含“””的元素您好,现在可以了,但是..我可以在没有ID或类的情况下这样做吗?意思是我可以用javascript找到文本并替换它吗?