使用javascript编写HTML时使用单引号
我在php文件中有以下代码:使用javascript编写HTML时使用单引号,javascript,html,Javascript,Html,我在php文件中有以下代码: <script> var myString = '{$data10}'; if (myString.charAt(0)) { document.write('<a target="_blank" href="https://www.somedomain.com/join?id={$data10}"> <img src="images/jo
<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'"
但是,不接受单引号。必须转义内部引号
onMouseOver="this.src=\"images/join-bimB.png\""
您可以使用转义单引号,如下所示
<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>
引号不会转义,这意味着您的引号将关闭第一个引号,使PHP变得混乱 要转义它,请使用内部单引号:
onMouseOver=\'this.src="images/join-bimB.png"\'
onMouseOver=\'this.src="images/join-bimB.png"\'