Url中的JavaScript变量不起作用

时间:2012-08-29 18:00:31

标签: javascript html variables

此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

3 个答案:

答案 0 :(得分:3)

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

document.getElementById("Image").innerHTML = 
    "<img style='float:left' width='100%' height='100%' src='http://host/"+ActionItemsVar+".jpg'alt='' title=''>";

答案 1 :(得分:2)

使用“而不是'

document.getElementById("Image").innerHTML = "<img style='float:left' width='100%' height='100%' src='http://host/"+ActionItemsVar+".jpg'alt='' title=''>";

答案 2 :(得分:1)

你的报价很混乱。改为

document.getElementById("Image").innerHTML = "<img style='float:left' width='100%' height='100%' src='http://host/"+ActionItemsVar+".jpg'alt='' title=''>";