最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascripts not working when <!doctype html> inserted in correct format - Stack Overflow

programmeradmin0浏览0评论

Im developing a web page and im using some javascripts on the pages to move some html elements, but i found that javascript code is not working when <!doctype html> or any type of <!doctype> s added to the top of the page ! what is this error?

I search on google and i found <!doctype html> is the correct format even stackoverflow source code has it but when i used it javascripts not working but when it removed script working fine & html object is moving but my styles are gone away because of removing <!doctype html>

<!doctype html>   when this line removed it works(html element is moving)
<html>
<head>
    <script type="text/javascript">

        function scrollDiv(){
                  var obj = document.getElementById("movingDiv");
                  obj.style.left = 100;
                  obj.style.top = 10;
                  }

                document.onclick = scrollDiv;

    </script>
</head>
<body  class="body">
<div class="maindiv" id="mainDiv">
  <div class="html_js_code" style="text-align:center;">

  <div id="movingDiv" style="position:absolute;left:100pt;top:10pt;border:1px blue 

outset;width:160px;background-color:lightyellow;font-weight:bold;">Moving Div</div>
</div>
</div>
</body>
</html>

Im developing a web page and im using some javascripts on the pages to move some html elements, but i found that javascript code is not working when <!doctype html> or any type of <!doctype> s added to the top of the page ! what is this error?

I search on google and i found <!doctype html> is the correct format even stackoverflow source code has it but when i used it javascripts not working but when it removed script working fine & html object is moving but my styles are gone away because of removing <!doctype html>

<!doctype html>   when this line removed it works(html element is moving)
<html>
<head>
    <script type="text/javascript">

        function scrollDiv(){
                  var obj = document.getElementById("movingDiv");
                  obj.style.left = 100;
                  obj.style.top = 10;
                  }

                document.onclick = scrollDiv;

    </script>
</head>
<body  class="body">
<div class="maindiv" id="mainDiv">
  <div class="html_js_code" style="text-align:center;">

  <div id="movingDiv" style="position:absolute;left:100pt;top:10pt;border:1px blue 

outset;width:160px;background-color:lightyellow;font-weight:bold;">Moving Div</div>
</div>
</div>
</body>
</html>
Share Improve this question edited May 31, 2016 at 6:55 Serenity 36.8k21 gold badges123 silver badges116 bronze badges asked Jul 8, 2012 at 15:32 Naveen GamageNaveen Gamage 1,88412 gold badges33 silver badges52 bronze badges 1
  • 1 Your logic, your way of thinking is good. Only the conclusion is wrong: the error must be in your Javascript code. – kapa Commented Jul 8, 2012 at 15:39
Add a ment  | 

2 Answers 2

Reset to default 9

Without a doctype, the browser assumes that unlabelled lengths are in pixels. With a doctype, they require an explicit unit.

obj.style.left = "100px";
obj.style.top = "10px";

you also forgot to put the parenthesis at the end of the function you called. I'm also pretty sure you passed it incorrectly. I believe it should look like this:

document.onclick = scrollDiv();

or

document.onclick(scrollDiv());
发布评论

评论列表(0)

  1. 暂无评论