js学习笔记

DOM是什么:

DOM 是 W3C(万维网联盟)的标准。

DOM 定义了访问 HTML 和 XML 文档的标准:

“W3C 文档对象模型 (DOM) 是中立于平台和语言的接口,它允许程序和脚本动态地访问和更新文档的内容、结构和样式。”

W3C DOM 标准被分为 3 个不同的部分:

  • 核心 DOM - 针对任何结构化文档的标准模型
  • XML DOM - 针对 XML 文档的标准模型
  • HTML DOM - 针对 HTML 文档的标准模型

DOM对象:

一些常用的 HTML DOM 方法:

  • getElementById(id) - 获取带有指定 id 的节点(元素)
  • appendChild(node) - 插入新的子节点(元素)
  • removeChild(node) - 删除子节点(元素)

一些常用的 HTML DOM 属性:

  • innerHTML - 节点(元素)的文本值
  • parentNode - 节点(元素)的父节点
  • childNodes - 节点(元素)的子节点
  • attributes - 节点(元素)的属性节点
getElementById() 返回带有指定 ID 的元素。
getElementsByTagName() 返回包含带有指定标签名称的所有元素的节点列表(集合/节点数组)。
getElementsByClassName() 返回包含带有指定类名的所有元素的节点列表。
appendChild() 把新的子节点添加到指定节点。
removeChild() 删除子节点。
replaceChild() 替换子节点。
insertBefore() 在指定的子节点前面插入新的子节点。
createAttribute() 创建属性节点。
createElement() 创建元素节点。
createTextNode() 创建文本节点。
getAttribute() 返回指定的属性值。
setAttribute() 把指定属性设置或修改为指定的值。

setTImeout(code,millisec)

code:调用的js串

millisec:等待毫秒数

javascript:

js的许多基本语法都与C语言类似。

排序:

var arr = new Array(6) 创建新的数组
arr.sort() 数组排序(文字排序)
arr.sort(sortNumber)) (数字排序)

Math.random:生成0-1的随机数

Math.max()  Math.min()

算数值

JavaScript 提供 8 种可被 Math 对象访问的算数值:

  • 常数
  • 圆周率
  • 2 的平方根
  • 1/2 的平方根
  • 2 的自然对数
  • 10 的自然对数
  • 以 2 为底的 e 的对数
  • 以 10 为底的 e 的对数

这是在 Javascript 中使用这些值的方法:(与上面的算数值一一对应)

  • Math.E
  • Math.PI
  • Math.SQRT2
  • Math.SQRT1_2
  • Math.LN2
  • Math.LN10
  • Math.LOG2E
  • Math.LOG10E

 RegExp 对象

text()

var patt1=new RegExp("3");  

document.write(patt1.test("The best things in life are free")); //匹配是否有3

exec()

var patt1=new RegExp("ea");
document.write(patt1.exec("The best things in life are free")); //查看是否有ea,若没有输出null

可以向 RegExp 对象添加第二个参数,以设定检索。例如,如果需要找到所有某个字符的所有存在,则可以使用 "g" 参数 ("global")。

var patt1=new RegExp("e","g");
do
{
result=patt1.exec("The best things in life are free");
document.write(result);
}
while (result!=null) 

compile() 方法用于改变 RegExp。

compile() 既可以改变检索模式,也可以添加或删除第二个参数。

var patt1=new RegExp("e");

document.write(patt1.test("The best things in life are free"));

patt1.compile("d");

document.write(patt1.test("The best things in life are free"));

 Window - 浏览器对象模型

  • window.open() - 打开新窗口
  • window.close() - 关闭当前窗口
  • window.moveTo() - 移动当前窗口
  • window.resizeTo() - 调整当前窗口的尺寸

Window Location

window.location 对象在编写时可不使用 window 这个前缀。

一些例子:

  • location.hostname 返回 web 主机的域名
  • location.pathname 返回当前页面的路径和文件名
  • location.port 返回 web 主机的端口 (80 或 443)
  • location.protocol 返回所使用的 web 协议(http:// 或 https://)
  • location.href (返回页面的url)
  • location.pathname (返回URL的路径名)
  • location.assign() (加载新的文档)

Window History

window.history 对象在编写时可不使用 window 这个前缀。

为了保护用户隐私,对 JavaScript 访问该对象的方法做出了限制。

一些方法:

  • history.back() - 与在浏览器点击后退按钮相同
  • history.forward() - 与在浏览器中点击按钮向前相同

 JavaScript 中三种消息框:警告框、确认框、提示框

alert("警告框!!");

确认框:

<script type="text/javascript">
function show_confirm()
{
  var r=confirm("Press a button!");//此处确认框选择
  if (r==true){  //选确定时
    alert("You pressed OK!");
  }
  else{
    alert("You pressed Cancel!");
  }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show a confirm box" />

提示框:

<script type="text/javascript">
function disp_prompt()
{
  var name=prompt("请输入您的名字","Bill Gates")//输入信息
  if (name!=null && name!=""){
    document.write("你好!" + name + " 今天过得怎么样?")
  }
}
</script>
</head>
<body>

<input type="button" onclick="disp_prompt()" value="显示提示框" />

提示输入信息

 Cookie

  又是有关cookie的东西,第一次接触到时在使用cookie写python脚本的时候。当时需要用户登录才能访问到网页。

  原来cookie就是这么一把钥匙,让用户登录。

  官方的语言描述:存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值。

  使用下面代码检查是否已设置过cookie

function getCookie(c_name){
    if (document.cookie.length>0){ 
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1){ 
            c_start=c_start + c_name.length+1 
        c_end=document.cookie.indexOf(";",c_start)
        if (c_end==-1) c_end=document.cookie.length
        return       unescape(document.cookie.substring(c_start,c_end))
        } 
    }
    return ""
}
原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9577203.html