JavaScript学习笔记

来源:互联网 发布:python和java的区别 编辑:程序博客网 时间:2024/05/22 16:53

弹出对话框

confirm(“输入问题”); //返回一个Boolean值,如 var message=confirm(“输入问题”);

prompt(“请输入你的成绩”,”什么”) //返回一个变量值 str1: 要显示在消息对话框中的文本,不可修改 str2:文本框中的内容,可以修改,str即为默认返回的内容,不写为null

window.open(‘http://www.imooc.com‘,’_blank’,’width=600,height=400,menubar=no,toolbar=no, status=no,scrollbars=yes’)
//打开的链接地址,新窗口打开,高度,宽度,菜单栏,工具栏,状态栏,滚动栏

var mywin=window.open(“http://www.imooc.com“); mywin.close(); //关闭窗口

document.getElementById(“”); //通过id获取元素

mychar.style.color=”red”; mychar.style.backgroundColor=”#CCC” mychar.style.width=”300px”; document.getElementById(“txt”).style.cssText = “”;

txt.style.display=”none”或”block”
//样式修改

p1.className=”one” //为类设置样式

鼠标经过事件(onmouseover) 鼠标移开事件(onmouseout) 光标聚焦事件(onfocus) 失焦事件(onblur) 内容选中事件(onselect)

文本框内容改变事件(onchange) 加载事件(onload) 卸载事件(onunload)—(window.onunload)

stringObject.indexOf(substring, startpos) 字符串检索,检索的字符,从第几个位置开始

stringObject.split(separator,limit) 字符串分割 ,分割条件,分割几次

提取字符串substring(start,stop) 起止

提取指定数目的字符substr(startPos,length) 起 –长度

JavaScript 计时器

setInterval(clock,1000) 执行的函数,间隔的时间 取消计时器clearInterval()参数为setInterval 对象 setTimeout(代码,延迟时间);

取消计时器clearTimeout() 参数为延时器的id

window.history.back====window.history.go(-1) window.history.forward();==window.history.go(1);

location.href 返回当前的url host主机名和端口号 hostname 主机名 port端口号

navigator.appName 浏览器代码名的字符串表示
navigator.appCodeName 返回浏览器的名称
navigator.appVersion 返回浏览器的平台和版本信息
navigator.platform 返回运行浏览器的操作系统平台
navigator.userAgent 返回有客户机发送服务器的userAgent头部的值

userAgent navigator.userAgent

获取屏幕信息 window.screen.属性
avaikHeight 窗口可以使用的屏幕高度
availWidth 窗口可以使用的屏幕宽度
colorDepth 用户浏览器表示的颜色位数
pixelDepth 用户浏览器表示的颜色位数
height 屏幕的高度
width 屏幕宽度

document.getElementsByName(name) 通过name获得元素

document.getElementsByTagName(Tagname) 返回带有指定标签名的节点对象的集合。返回元素的顺序是它们在文档中的顺序。

getAttribute()方法 elementNode.getAttribute(name) 通过元素节点的属性名称获取属性的值。

elementNode.setAttribute(name,value) 设置属性

document.write(mylist.parentNode.parentNode.parentNode.lastChild.innerHTML);

nodeObject.nextSibling nodeObject.previousSibling 访问兄弟节点 参数为节点

appendChild(newnode) 插入节点

createElement(“li”); createTextNode(“PHP”); 插入节点insertBefore() nodeObject.removeChild(node)移除节点

将加粗改为斜体 a标签里可以为一个JavaScript方法

oldnode.parentNode.replaceChild(newnode,oldnode); 节点取代

createTextNode() 方法创建新的文本节点,返回新创建的 Text 节点。