用 Javascript 替换单词而不删除图像 [英] Replace words with Javascript without removing the image
问题描述
我想在这个 p 标签中用skägget"替换morrhåren"这个词.我不应该更改 HTML-kod 中的任何内容,只能在我的 js 文件中更改.如何使用 Javascript 更改这些单词而不使图像消失?HTML-kod
I want to replace the words "morrhåren" with "skägget" in this p-tag. I’m not supposed to change anything in the HTML-kod, only in my js-file. How can I manage to change these words using Javascript and not making the image disappear? The HTML-kod
<p><img src="http://blogg.wikki.se/wp-content/upploads/2009/06/catbyBaikal.jpg" align="right">
Är <span>katt</span>en skrämd lägger den morrhåren bakåt, detsamma gör den vid slagsmål för att skydda morrhåren. Om <span>katt</span>en är en nyfiken vinklar den morrhåren framåt.<p>
推荐答案
您可以使用 <p>
元素的 innerHTML 属性 (document.getElementById("/**enter<p> 元素的 id*/").innerHTML
) 以获取(通过将其分配给变量)或设置(通过将新的字符串值分配给属性)任何 HTML 元素的全部内容.
You can use the innerHTML property of your <p>
element (document.getElementById("/**enter id of your <p> element*/").innerHTML
) to fetch (by assigning it to a variable) or to set (by assigning a new string value to the property) the entire content of any HTML element.
这篇关于用 Javascript 替换单词而不删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!