2012-08-29 97 views
-4

這段JavaScript代碼工作得很好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 

回答

1

您的報價很混亂。更改爲

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

使用「,而不是」

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

你必須退出雙引號不是單一的:

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