您的位置:首页 > 其它

markdown编辑器实现笔记

2015-11-24 14:29 429 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: