[英]Javascript. Replace HTML content of a Div
So I am currently building a website where when you click on a small image it displays bigger above. 因此,我目前正在建立一个网站,当您单击一个小图像时,它会在上方显示较大的图像。 Along with the image being enlarged text overlays the image describing it and I can't seem to get the code to change the text right.
随着图像被放大,文本覆盖了描述它的图像,我似乎无法获得正确更改文本的代码。 The text is more complicated as it uses spans etc so i have to place HTML code to and it just displays the text without the formatting.
文本更复杂,因为它使用跨度等,因此我必须将HTML代码放置到其中,并且它仅显示文本而无需设置格式。
// Image 1 - Football function change1() { document .getElementById("mainPic") .src = "http://files.stv.tv/imagebase/170/623x349/170248-lossiemouth-fc.jpg" ; document .getElementById("imageCaption") .innerHTML = "<span>Lossiemouth Football Club<span class='spacer'></span><br /><span class='spacer'></span>Come watch a game of football!</span>" ; } // Image 2 - Football function change2() { document .getElementById("mainPic") .src = "http://www.visitscotland.com/wsimgs/Course%206_704927777.JPG[ProductMain]" ; document .getElementById("imageCaption") .innerHTML = "<span>Golf Course<span class='spacer'></span><br /><span class='spacer'></span>Come play a couple rounds of golf!</span>" ; }
<div id="imageCaption"> <h2 align="left" style="padding-left: 105px;"> <span>Lossiemouth Football Club <span class="spacer"></span> <br /> <span class="spacer"></span> Come watch a game of football! </span> </h2> </div>
Screen shot examples: Before 屏幕截图示例:之前
After
后
You are replacing the <h2>
tag. 您将替换
<h2>
标签。 Just move your id to that tag or create a new id and use it to set innerHTML
只需将您的ID移至该标签或创建一个新ID并使用它来设置
innerHTML
The code you are inserting with the javascript is different than the code that is initially in the div. 您使用javascript插入的代码与div中最初插入的代码不同。
change1()
<h2 align="left" style="padding-left: 105px;"><span>Lossiemouth Football Club<span class="spacer"></span><br /><span class="spacer"></span>Come watch a game of football!</span></h2>
is becoming 正在变得
<span>Lossiemouth Football Club<span class='spacer'></span><br /><span class='spacer'></span>Come watch a game of football!</span>
You need to either add the <h2>
tag or change what you reference with document.getElementById
您需要添加
<h2>
标记或更改您使用document.getElementById
引用的document.getElementById
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.