Javascript JavascriptsinnerHTML 不适用于图像,但适用于文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11266104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:12:18  来源:igfitidea点击:

Javascripts innerHTML not working for images, but works with text?

javascriptinnerhtml

提问by FoxyFish

Ok, i have a javascript function which toggles the innerhtml of a div tag when a user changes the option of a select dropdown box..

好的,我有一个 javascript 函数,当用户更改选择下拉框的选项时,它会切换 div 标签的innerhtml。

It all works fine with text, but with an image tag it stops working?

这一切都适用于文本,但使用图像标签它停止工作?

Working Example...

工作示例...

function toggle(opt) {
        var d = document.getElementById('div_tag');
        if (opt == '5') {
                d.innerHTML = 'FIVE';
        }
        else if (opt == '4') {
                d.innerHTML = 'FOUR';
        }
        etc...
}

Not Working Example...

不工作的例子...

function toggle(opt) {
        var d = document.getElementById('div_tag');
        if (opt == '5') {
                d.innerHTML = '<img src='path/img1.jpg'><img src='path/img2.jpg'>';
        }
        else if (opt == '4') {
                d.innerHTML = '<img src='path/img2.jpg'><img src='path/img1.jpg'>';
        }
        etc...
}

This is what i have on my select and div tags.

这就是我的 select 和 div 标签。

<select onchange='toggle(this.value);'>
<div id='div_tag'></div>

Anyone tell me what i'm doing wrong here cos i am stumped.. why would one work and not the other when all that is different is one been the other being text??

任何人都告诉我我在这里做错了什么,因为我被难住了..为什么一个工作而不是另一个工作,当所有不同的是另一个是文本?

Thanks.

谢谢。

回答by Ryan Flood

It seems to me like you forgot to escape the quotes surrounding your image paths, and its not reading your string correctly, try this

在我看来,您忘记转义图像路径周围的引号,并且没有正确读取您的字符串,试试这个

function toggle(opt) {
    var d = document.getElementById('div_tag');
    if (opt == '5') {
            d.innerHTML = '<img src=\'path/img1.jpg\'><img src=\'path/img2.jpg\'>';
    }
etc...

回答by Ashwin Singh

Do these changes, replace 'with "at the beginning and end.

难道这些变化,替换'"的开头和结尾。

function toggle(opt) {
        var d = document.getElementById('div_tag');
        if (opt == '5') {
                d.innerHTML = "<img src='path/img1.jpg'><img src='path/img2.jpg'>";
        }
        else if (opt == '4') {
                d.innerHTML = "<img src='path/img2.jpg'><img src='path/img1.jpg'>";
        }
        etc...
}

You were actually using single quote instead of double quotes. path/img1.jpg, path/img2.jpgwasn't being treated part of your string earlier. That was the problem.

您实际上使用的是单引号而不是双引号。path/img1.jpg,path/img2.jpg之前没有被视为字符串的一部分。那就是问题所在。

UPDATEFor you php problem do this:

更新对于您的 php 问题,请执行以下操作:

function toggle(opt) {
        var d = document.getElementById('div_tag');
        if (opt == '5') {
                d.innerHTML = '<img src='+'path/img1.jpg'+'><img src='+'path/img2.jpg'+'>';
        }
        else if (opt == '4') {
                d.innerHTML = '<img src='+'path/img2.jpg'+'><img src='+'path/img1.jpg'+'>';
        }
        etc...
}