javascript学习经历(一):变量名,单双引号注意事项

1、javascript单双引号

  • javascript中双引号与单引号是都适用的,但在html元素中写javascript代码时需要使用单引号 ’ ’ 其他时候双引号与单引号没有区别
<h2>Body 中的 JavaScript</h2>

<p id="demo">一个段落。</p>

<button type="button" onclick="document.getElementById('demo').innerHTML = '你错了'">试一试</button>

/*-------两者时相同的----------*/


2、document.write()

  • doucment.write()用于输出内容
  • 例:document.write(5 + 6); 即输出11,可以写在body中间的任何地方

<!DOCTYPE html>
<html>
<body>
<h2>我的第一张网页</h2>
<p>我的第<script>
document.write(5 + 6);
</script>一个段落。</p>
<script>
document.write(5 + 6);
</script>![在这里插入图片描述](https://img-blog.csdnimg.cn/20190815165331535.png)
</body>
</html>

上例的结果是

javascript学习经历(一):变量名,单双引号注意事项_第1张图片

注意:document.write()在页面加载结束后运行,会清除所有html元素。

将document.write()写在底部body标签处或者js代码为window.οnlοad=function(){}的

会将html清除

你可能感兴趣的:(javascript学习经历(一):变量名,单双引号注意事项)