onmousemove和onmouseout事件的调用,和js使用双引号、单引号的时候应该注意的问题

时间:2023-03-09 07:14:27
onmousemove和onmouseout事件的调用,和js使用双引号、单引号的时候应该注意的问题

使用js的时候,统一使用双引号,然后通过反斜杠进行转义

①如果同时使用单引号、和双引号的情况下容易出现问题,导致标签中表示的事件不能调用,

②导致由于标签没有封口而出现样式布局错误

 <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS教程:鼠标悬停时显示文字或显示图片</title>
<script type="text/javascript">
function showPic(sUrl) {
alert(sUrl);
var x, y;
x = event.clientX;
y = event.clientY;
document.getElementById("Layer1").style.left = (x - 60).toString() + "px";
document.getElementById("Layer1").style.top = (y - 100).toString() + "px";
document.getElementById("Layer1").innerHTML = "<img src=\"" + sUrl + "\">";
document.getElementById("Layer1").style.display = "block";
}
function hiddenPic() {
document.getElementById("Layer1").innerHTML = "";
document.getElementById("Layer1").style.display = "none";
}
</script>
</head>
<body>
<div style="position: relative">
<div id="Layer1" style="display: none; position: absolute; z-index: 1;"></div>
</div>
<img src="pic/QQ截图20150721092858.jpg" onmouseout="hiddenPic()" onmousemove="showPic(this.src)" title="wowowowo" style="margin-top: 300px" />
<div onmouseout="hiddenPic()" onmousemove="showPic('pic/QQ截图20150721092858.jpg')" style="margin-top: 300px; width: 200px; height: 100px; background-color: blue" />
</body>
</html>

对于上述代码,如果ShowPic中的内容,没有使用单引号括起来的情况下,那么将会导致无法调用showPic,因为如果不使用单引号的情况下就会导致将其解析为表达式

注意其中的onmousemove和onmouseout事件的使用