0
你能告訴我爲什麼我的JS不工作?當onclick被觸發時div不會移動?
http://prime.programming-designs.com/designs/map/
它假設移動標記DIV鼠標XY,當我在地圖上點擊。
你能告訴我爲什麼我的JS不工作?當onclick被觸發時div不會移動?
http://prime.programming-designs.com/designs/map/
它假設移動標記DIV鼠標XY,當我在地圖上點擊。
打開控制檯,你會看到:
Uncaught TypeError: Cannot read property 'MouseX' of undefined
看來,在line 33
在你的JavaScript代碼,document.Show
是undefined
。
document.Show.MouseX.value = tempX;
當你click
,你會得到這樣的:
Uncaught TypeError: Cannot read property 'style' of null
各地line 52
其中mark
是null
:
<div id="map" onclick="
mark.style.left = tempX + 'px';
mark.style.top = tempY + 'px';
">
<div id="mark"></div>
您可以向下移動整個<script>
標籤朝下方,放置它只是在關閉</body>
標籤內,以便您的document.getElementById('mark');
會找到這個元素。
像這樣:
<body>
<div id="map" onclick="
mark.style.left = tempX + 'px';
mark.style.top = tempY + 'px';
">
<div id="mark"></div>
</div>
<script type="text/javascript">
<!--
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var mark = document.getElementById('mark');
var tempX = 0;
var tempY = 0;
function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
}
else { // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}
document.Show.MouseX.value = tempX;
document.Show.MouseY.value = tempY;
return true;
}
//-->
</script>
</body>
如何在Firefox中打開控制檯?另外,我該如何解決這些問題?我是JS的初學者。 – CyanPrime 2011-01-20 23:32:24