簡體   English   中英

HTML Doctype標簽會影響javascript嗎?

[英]Html Doctype tag affects javascript?

我注意到html / javascript的奇怪行為。

我有這個JavaScript代碼

window.onload = function(){

var pmap = document.getElementById('pmap'); 

var marker_sl = document.getElementById('marker_sl');
var marker_uk = document.getElementById('marker_uk');

    marker_sl.style.left = pmap.width * 70.5 / 100;
    marker_sl.style.top = pmap.height * 48 / 100;

    marker_uk.style.left = pmap.width * 44 / 100;
    marker_uk.style.top = pmap.height * 14.5 / 100;   
};

當我添加時它不起作用

<!DOCTYPE html>

要么

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

標簽。 否則它將按預期工作。

我在stackoverflow中發現了這個問題 但這似乎沒有幫助。

為什么會這樣。 你能告訴我嗎。

好吧,這是我的整個HTML文件

<!DOCTYPE html>
<html>
    <head></head>

<body>
<article>
<img src="images/price_map.jpg" id="pmap" />
<a href="#" id="marker_sl" style="position:absolute;display:block;"><img src="images/marker.png"  /></a>
<a href="#" id="marker_uk" style="position:absolute;display:block;"><img src="images/marker.png"  /></a>
</article>
</body>
<script type="text/javascript">

window.onload = function(){

    var pmap = document.getElementById('pmap'); 

    var marker_sl = document.getElementById('marker_sl');
    var marker_uk = document.getElementById('marker_uk');

        marker_sl.style.left = pmap.width * 70.5 / 100;
        marker_sl.style.top = pmap.height * 48 / 100;

        marker_uk.style.left = pmap.width * 44 / 100;
        marker_uk.style.top = pmap.height * 14.5 / 100;   

};
</script>
</html>

解決的代碼

<!DOCTYPE html>
<html>
    <head></head>

<body>
<article>
<img src="images/price_map.jpg" id="pmap" />
<a href="#" id="marker_sl" style="position:absolute;display:block;"><img src="images/marker.png"  /></a>
<a href="#" id="marker_uk" style="position:absolute;display:block;"><img src="images/marker.png"  /></a>
</article>
</body>
<script type="text/javascript">

window.onload = function(){

    var pmap = document.getElementById('pmap'); 

    var marker_sl = document.getElementById('marker_sl');
    var marker_uk = document.getElementById('marker_uk');

        marker_sl.style.left = (pmap.width * 70.5 / 100)+"px";
        marker_sl.style.top = (pmap.height * 48 / 100)+"px";

        marker_uk.style.left = (pmap.width * 44 / 100)+"px";;
        marker_uk.style.top = (pmap.height * 14.5 / 100)+"px";   

};
</script>
</html>

文檔聲明對javascript不應有任何影響。 發布html文件,或者打開firebug,看看使用文檔聲明可以運行哪些書籍。

lefttop CSS屬性需要一個單位,例如“ px”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM