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

Im developing a web page and im using some javascripts on the pages to move some html elements, but i f

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());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745096372a4611002.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信