Web之一 JS

来源:互联网 发布:学拼音打字软件下载 编辑:程序博客网 时间:2024/05/16 11:02

1.js各种弹窗

1).alertalert("hello");2).confirmif(confirm("how are you?") == true){document.write("好");}else{document.write("不好");}3).promptvar scope = prompt("请输入你的成绩","100");alert("我的成绩是: " + "scope");

2.打开、关闭窗口

var win = window.open(url);        win.close();

3.事件

1)onclick 鼠标点击事件<input type="button" onclick="message()">2)onmouseover 鼠标经过事件<input type="button" onmouseover="message()">3)onmouseout 鼠标移开事件<input type="button" onmouseout="message()">4)onfocus 光标聚焦事件<input type="button" onfocus="message()">5)onblur 失焦事件<input type="button" onblur="message()">5)onselect 选中事件<textarea onselect="message()">hello!</textarea>5)onchange 内容改变事件<input type="text" onchange="message()">5)onload 加载事件<body onload="message()">  欢迎学习JavaScript。</body>5)onunload 卸载事件<body onunload="message()">  欢迎学习JavaScript。</body>

4.内置对象

4.1 String对象

var str = "Hello World";//全部大写str.toUpperCase();//全部小写str.toLowerCase();//返回指定位置的字符str.charAt(2);//返回字符串首次出现的位置,下面为o第二次出现的位置,第二个参数为开始索引的位置str.indexOf("o",str.indexOf("o")+1);//切割str.split(";");//截取字符串str.substring(startPos,endPos);str.substr(startPos,length);

4.2 Math对象

//PI常量Math.PI;//向上取整Math.ceil(6.3);//7//向下取整Math.floor(6.3);//6//四舍五入Math.round(6.3);//随机数 0~1Math.random();


4.3 数组对象

//定义数组var arr = new Array();var arr = [1,2,3];//数组连接arr1.concat(arr2,arr3);//指定分隔符连接数组arr.join(";");//数组排序arr.sort();//颠倒数组元素顺序arr.reverse();//从已有数组里返回选定的数组arr.slice(start,end);//从数组中删除或新增元素arr.splice(start,delNum,item1,item2)

4.4 Date对象

//新建对象var mydate = new Date();//年 mydate.getFullYear();mydate.setFullYesr(2016);//月 0~11mydate.getMonth();mydate.setMonth(0);//天1~31mydate.getDate();mydate.setDate(20);//小时 0~23mydate.getHours();//分 0~59mydate.getMinutes();//秒 0~59mydate.getSeconds();//星期 1~7mydate.getDay();mydate.setDay(1);


5.浏览器对象(window对象)

5.1 setInterval() setTimeout() 延时器 

     //setInterval() clearInterval()    function clock(){       var time=new Date();                        document.getElementById("clock").value = time;    }    var tt = setInterval(clock,1000);    <input type="button" value="Stop"  onclick="clearInterval(tt)"/>

5.2 history()

//返回上一个页面window.history.back();window.history.go(-1);//返回下一个页面window.history.forward();window.history.go(1);//返回当前页面之后浏览过的第三个历史页面window.history.go(3);

5.3 location()

//urlwindow.location.href//锚点window.location.hash//主机名和端口号window.location.host//主机名window.location.hostname//端口号window.location.post

6.DOM对象

5.1 获取元素

//根据id取元素document.getElementById("id");//根据name取元素document.getElementsByName("name");//根据标签名称去元素,如p、a等document.getElementsByTagName("tagName");

5.2 节点

        //getAttribute() 根据节点的属性名称获取属性值 var node = document.getElementById("id");var title = node.getAttribute("title");//setAttribute() 设置属性值node.setAttribute("title","WEB前端技术");//节点的属性var nodes = document.getElementsByTagName("li");for(var i=0; i<nodes.length; i++){documents.write(nodes[i].nodeName); //LIdocuments.write(nodes[i].nodeValue); //nulldocuments.write(nodes[i].nodeType); //1}//创建节点document.createElement(tagName);//子节点var childNodes = node.childNodes;//子节点的第一项和最后一项node.firstChild;node.lastChild;//父节点node.parentNode;//兄弟节点node.nextSibling;node.previousSibling;  //添加节点  appendChild()var otest = document.getElementById("test");  var newnode = document.createElement("li");newnode.innerHTML = "PHP";otest.appendChild(newnode);//插入节点 insertBefore()parantNode.insertBefore(newNode,node);//删除节点node.removeChild(node);//替换节点node.replaceChild (newnode,oldnode);//创建文本节点 createTextNode()var pNode = document.createElement("p");        var text = document.createTextNode("I love JavaScript!");        pNode.appendChild(text);        document.body.appendChild(pNode);




0 0