2014-02-28 83 views
-1

編寫HTML時,我有這個代碼的PHP文件中:使用singlequotes的使用JavaScript

<script> 

var myString = '{$data10}'; 
if (myString.charAt(0)) { 
    document.write('<a target="_blank" 
         href="https://www.somedomain.com/join?id={$data10}"> 
        <img src="images/join-bim.png" width="300" height="74" 
         onMouseOut="this.src="images/join-bim.png"" 
         onMouseOver="this.src="images/join-bimB.png"" /></a>'); 
} else { 
    document.write('<a target="_blank" 
         href="https://www.somedomain.com/join?id=111111"> 
        <img src="images/join-bim.png" width="300" height="74" 
         onMouseOut="this.src="images/join-bim.png"" 
         onMouseOver="this.src="images/join-bimB.png"" /></a>'); 
} 
</script> 

我曾嘗試:由於報價沒有嵌套

onMouseOut="this.src="images/join-bim.png"" 

,但遇到的問題。

正因爲如此,我又試圖:

onMouseOut="this.src='images/join-bim.png'" 

但是,單引號不被接受。

回答

0

您可以使用轉義單引號,因爲你可以看到下面

<script> 
var myString = '{$data10}'; 
    if(myString.charAt(0)){ 
     document.write('<a target="_blank" href="https://www.somedomain.com/join?id={$data10}"><img src="images/join-bim.png" width="300" height="74" onMouseOut="this.src=\'images/join-bim.png\'" onMouseOver="this.src=\'images/join-bimB.png\'" /></a>'); 
    } 
    else { 
     document.write('<a target="_blank" href="https://www.somedomain.com/join?id=111111"><img src="images/join-bim.png" width="300" height="74" onMouseOut="this.src=\'images/join-bim.png\'" onMouseOver="this.src=\'images/join-bimB.png\'" /></a>'); 
    } 
</script> 
2

必須轉義內部引號

onMouseOver="this.src=\"images/join-bimB.png\"" 
0

引號不被轉義,這意味着你的引號內被關閉第一次報價,使PHP感到困惑。

爲了躲避它,使用你的內心,單引號:

onMouseOver=\'this.src="images/join-bimB.png"\'