-4

这个 javascript 代码运行良好

src='http://host/150.jpg'

 // Function ActionItems Start
 function ActionItems(){
    var ActionItemsVar = document.getElementById("ActionItems").value;
        switch (ActionItemsVar ){
            case "150":
            ActionItems1Var ="ActionItems1";
            ActionItems2Var="ActionItems2";
            ActionItems3Var="ActionItems3";
            ActionItems4Var="ActionItems4";
            document.getElementById("Image").innerHTML = "<img style='float:left' width='100%' height='100%'
                            src='http://host/150.jpg' alt='' title=''>";
            break;
            ....
        }

    document.getElementById("ActionItems1").innerHTML = ActionItems1Var
    document.getElementById("ActionItems2").innerHTML = ActionItems2Var
    document.getElementById("ActionItems3").innerHTML = ActionItems3Var
    document.getElementById("ActionItems4").innerHTML = ActionItems4Var
 }
 // Function ActionItems End

但是在 url 代码中使用变量将不起作用

src='http://host/'+ActionItemsVar+'.jpg'

// Function ActionItems Start
    function ActionItems(){
        var ActionItemsVar = document.getElementById("ActionItems").value;
            switch (ActionItemsVar ){
                case "150":
                ActionItems1Var ="ActionItems1";
                ActionItems2Var="ActionItems2";
                ActionItems3Var="ActionItems3";
                ActionItems4Var="ActionItems4";
                document.getElementById("Image").innerHTML = "<img style='float:left' width='100%' height='100%'
                            src='http://host/'+ActionItemsVar+'.jpg'alt='' title=''>";
                break;

                ...
            }

        document.getElementById("ActionItems1").innerHTML = ActionItems1Var
        document.getElementById("ActionItems2").innerHTML = ActionItems2Var
        document.getElementById("ActionItems3").innerHTML = ActionItems3Var
        document.getElementById("ActionItems4").innerHTML = ActionItems4Var
    }
    // Function ActionItems End
4

3 回答 3

3

您必须退出双引号而不是单引号:

document.getElementById("Image").innerHTML = 
    "<img style='float:left' width='100%' height='100%' src='http://host/"+ActionItemsVar+".jpg'alt='' title=''>";
于 2012-08-29T18:05:20.490 回答
2

使用 " 而不是 '

document.getElementById("Image").innerHTML = "<img style='float:left' width='100%' height='100%' src='http://host/"+ActionItemsVar+".jpg'alt='' title=''>";
于 2012-08-29T18:04:52.280 回答
1

你的报价很混乱。改成

document.getElementById("Image").innerHTML = "<img style='float:left' width='100%' height='100%' src='http://host/"+ActionItemsVar+".jpg'alt='' title=''>";
于 2012-08-29T18:04:01.970 回答