JS学习文档

来源:互联网 发布:arm体系结构与编程 pdf 编辑:程序博客网 时间:2024/06/05 12:08

在朗沃的一年一来,学习到许多,收获了许多



HTML与JS,完美的搭配,让我领略到了前端的魅力,下面是我摘抄和整理的一些笔记以及借鉴他人的一些知识。在此谢谢别人无私的分享


1.在 JavaScript 中,document.write() 可用于直接向 HTML 输出流写内容。

<!DOCTYPE html><html><body><script>document.write(Date());</script></body></html>

<!DOCTYPE html>
<html>
<body>

<p>
JavaScript 能够直接写入 HTML 输出流中:
</p>

<script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
</script>

<p>
您只能在 HTML 输出流中使用 <strong>document.write</strong>。
如果您在文档已加载后使用它(比如在函数中),会覆盖整个文档。
</p>


</body>
</html>


提示:绝不要使用在文档加载之后使用document.write()(比如在函数中使用这会覆盖该文档。)

上面的例子是直接在script标签中输出,如果在函数中输出document.write() 会覆盖的,见下面的例子:

//大家可以直接复制过去,看看效果

<!DOCTYPE html>
<html>
<body>
<p>点击下面的按钮,循环遍历对象 "person" 的属性。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>


<script>
function myFunction()
{
var x;
var txt="";
var person={fname:"Bill",lname:"Gates",age:56}; 


for (x in person)
{
document.write(x+" ");   //注意此处:这样会覆盖掉下面的
 //document.getElementById("demo").innerHTML=txt;可以删除document.write(x+" "); 这句话,然后

//看看效果就明白了
alert("x= "+x);
txt=txt + person[x];
}


document.getElementById("demo").innerHTML=txt;
}
</script>
</body>
</html>


参考见:http://www.w3school.com.cn/js/js_intro.asp

http://www.w3school.com.cn/tiy/t.asp?f=js_intro_document_write


2.Window 尺寸

有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。

对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:

  • window.innerHeight - 浏览器窗口的内部高度
  • window.innerWidth - 浏览器窗口的内部宽度

对于 Internet Explorer 8、7、6、5:

  • document.documentElement.clientHeight
  • document.documentElement.clientWidth

或者

  • document.body.clientHeight
  • document.body.clientWidth

实用的 JavaScript 方案(涵盖所有浏览器):

实例

var w=window.innerWidth|| document.documentElement.clientWidth|| document.body.clientWidth;var h=window.innerHeight|| document.documentElement.clientHeight|| document.body.clientHeight;








0 0