JS基础之事件简介
2018-03-06 22:12
429 查看
JS基础
第一部分
innerHTML与innerText的区别: innerHTML会解析里面的标签,比如"<p>福</p>",innerHTML会识别p标签 对于innerText来说,p标签和福字没什么区别,会一同打印出来 可以通过:console.log(first.innerHTML)来打印引号中的内容,加以区分
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> #first{ border:1px red solid; width:200px; height:200px; background-color:blue; margin:50px auto 50px; } </style> </head> <body> <div id="first"></div> </body> <script> var first = document.getElementById('first'); first.style.backgroundColor="yellow"; document.getElementById('first').style.backgroundColor = "blue"; first.innerHTML = "<P>福</P>" first.style.transform = "rotate(45deg)"; console.log(first.innerHTML); console.log(first.innerText 4000 ); </script> </html>
结果截图
将first.innerHTML = "<P>福</P>"换成first.innerText = "<P>福</P>"
结果截图
第二部分
通过类名获取一组元素 var uNames = document.getElementsByClassName("uName"); uNames是所有类名为uName的集合(先当数组来使用) uNames[0].value = "我是一个输入框"; 获取并修改输入框的值使用value console.log(uNames[0].value); 通过标签名获取 var buttom = document.getElementsByTagName("input")[1];
第三部分
事件的使用 运行说明:点击bottom按钮,text文本框里的值会加1,first矩形会逐渐增大
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> #first{ border:1px red solid; width:200px; height:200px; background-color:blue; margin:50px auto 50px; } </style> </head> <body> <div id="first"></div> <input type="text" class="uName" /> <input type="button" value="点我啊" /> </body> <script> var first = document.getElementById("first"); first.style.backgroundColor="yellow"; document.getElementById('first').style.backgroundColor = "blue"; var a = 1; var uName = document.getElementsByTagName("input")[0]; var botton = document.getElementsByTagName("input")[1]; botton.onclick = function(){ uName.value = a++; first.style.transition = "1s"; first.style.width = 10*a + "px"; first.style.height= 5 * a + "px"; } </script> </html>
结果截图
第四部分
建立两个text窗体一个botton按钮,左边窗体输入一个数字,右边窗体输出是奇数还是偶数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> </style> </head> <body> <input type = "text" /> <input type = "button" value = "计算"/> <input type = "text"> </body> <script type="text/javascript"> var text1 = document.getElementsByTagName("input")[0]; var botton = document.getElementsByTagName("input")[1]; var text2 = document.getElementsByTagName("input")[2]; botton.onclick = function(){ var num = text1.value; if(num % 2 == 0){ text2.value = num + "是偶数"; }else if(num % 2 != 0){ text2.value = num + "是奇数"; }else{ text2.value = "请确认" } } </script> </html>
结果截图
第五部分
模仿百度一栏中的<我的关注 推荐 导航 体育 小说>这一行的实现原理, 要求每当鼠标点击时就变色,其余的保持同色如图:
实现代码1:初步实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type = "text/css"> .item{ background-color:red; width:100px; height:20px; float:left; margin-top:200px; margin-left:100px; text-align:center; } .item:hover{ background-color:yellow; } </style> </head> <body> <div class="item">我的关注</div> <div class="item">推荐</div> <div class="item">导航体育</div> <div class="item">体育</div> <div class="item">小说</div> </body> <script > items = document.getElementsByClassName("item"); items[0].onclick = function(){ for(var i = 0; i < items.length; i++){ items[i].style.backgroundColor = "red"; } this.style.backgroundColor = "blue"; } items[1].onclick = function(){ for(var i = 0; i < items.length; i++){ items[i].style.backgroundColor = "red"; } this.style.backgroundColor = "blue"; } items[2].onclick = function(){ for(var i = 0; i < items.length; i++){ items[i].style.backgroundColor = "red"; } this.style.backgroundColor = "blue"; } items[3].onclick = function(){ for(var i = 0; i < items.length; i++){ items[i].style.backgroundColor = "red"; } this.style.backgroundColor = "blue"; } items[4].onclick = function(){ for(var i = 0; i < items.length; i++){ items[i].style.backgroundColor = "red"; } this.style.backgroundColor = "blue"; } </script> </html>
实现代码:初步优化
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type = "text/css"> .item{ background-color:red; width:100px; height:20px; float:left; margin-top:200px; margin-left:100px; text-align:center; } .item:hover{ background-color:yellow; } </style> </head> <body> <div class="item">我的关注</div> <div class="item">推荐</div> <div class="item">导航体育</div> <div class="item">体育</div> <div class="item">小说</div> </body> <script > items = document.getElementsByClassName("item"); for(var j = 0; j < items.length; j++){ items[j].onclick = function(){ for(var i = 0; i < items.length; i++){ items[i].style.backgroundColor = "red"; } this.style.backgroundColor = "blue"; } } </script> </html>
实现代码:再次优化
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type = "text/css"> .item{ background-color:red; width:100px; height:20px; float:left; margin-top:200px; margin-left:100px; text-align:center; } .item:hover{ background-color:yellow; } </style> </head> <body> <div class="item">我的关注</div> <div class="item">推荐</div> <div class="item">导航体育</div> <div class="item">体育</div> <div class="item">小说</div> </body> <script > items = document.getElementsByClassName("item"); var last = items[0]; for(var j = 0; j < items.length; j++){ items[j].onclick = function(){ this.style.backgroundColor = "blue"; last.style.backgroundColor = "red"; last = this; } } </script> </html>
结果截图
相关文章推荐
- Node.js学习--基础知识(3)--回调函数与事件
- node.js零基础详细教程(4):node.js事件机制、node异步IO操作
- JS基础内容小结(event 鼠标键盘事件)(三)
- JS 学习笔记--JS中的事件对象基础
- 服务器端 js 利用JavaScript开发事件驱动的Web应用 – node.js简介
- 【Node.js基础篇】(一)Hello World和事件驱动编程
- JS事件基础
- JS中的事件类型和事件属性的基础知识
- 网页基础第4课之“简单的JS与CSS结合--鼠标移入移出事件”
- 【C#基础】之WinForm窗体常用控件的属性、方法及事件简介 .
- js事件应用--基础(事件对象、鼠标事件、键盘事件)
- 前端基础 JS DOM事件(2)
- 利用JavaScript开发事件驱动的Web应用 – node.js简介
- java基础之标签、按钮和按钮事件简介
- JS 事件基础 事件绑定函数
- js实现类似jquery基础功能 简单选择器/事件/属性
- JS基础回顾,小练习(Event事件)
- JS基础学习四:绑定事件
- JS 基础事件的用法
- js事件:Ajax基础