Hiroki

大部分笔记已经转移到 https://github.com/hschen0712/machine_learning_notes ,QQ:357033150, 欢迎交流

markdown编辑器实现笔记

1.js代码放在head和body的区别

<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.8/ace.js" type="text/javascript" charset="utf-8"></script>
  <script type="text/javascript" src="./js/jquery/jquery.min.js"></script>
  <script>
    var templateEditor = document.getElementById("templateEditor");
    templateEditor.innerHTML="hello world";
  </script>
</head>

<body>
   <div id='templateEditor' style='height:500px;'></div>
</body>
</html>

js代码如果放在head,则先于body定义,如果代码里要对body的元素进行修改,这时body的元素还没有定义,则会出现undefined错误。
正确的写法应该是:

<body>
   <div id='templateEditor' style='height:500px;'></div>
   <script>
     var templateEditor = document.getElementById("templateEditor");
     templateEditor.innerHTML="hello world";
   </script>
</body>

即定义之后执行。
或者用jquery修改

  <script>
    $(document).ready(function() {
        $("#templateEditor").html("hello world");
    });
  </script>

posted on 2015-11-24 14:29  Hiroki  阅读(285)  评论(0编辑  收藏  举报

导航