01-隱藏顯示二維碼
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.nodeSmall {
width: 50px;
height: 50px;
background: url(images/bgs.png) no-repeat -159px -51px;
position: fixed;
right: 10px;
top: 40%;
}
.erweima {
position: absolute;
top: 0;
left: -150px;
}
.nodeSmall a {
display: block;
width: 50px;
height: 50px;
}
.hide {
display: none;
}
.show {
display: block;
}
</style>
<script>
window.onload = function () {
//需求:鼠標(biāo)放到a鏈接上,顯示二維碼(添加show類名,去掉hide類名)
// 鼠標(biāo)移開(kāi)a鏈接,隱藏二維碼(添加hide類名,去掉show類名)
//步驟:
//1.獲取事件源和相關(guān)元素
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序
//1.獲取事件源和相關(guān)元素
var a = document.getElementsByTagName("a")[0];
var div = document.getElementsByClassName("erweima")[0];
//2.綁定事件
a.onmouseover = fn1;
a.onmouseout = fn2;
//定義方法
function fn1() {
//3.書寫事件驅(qū)動(dòng)程序
div.className = "erweima show";
// div.className = div.className.replace("hide","show");
}
function fn2() {
div.className = "erweima hide";
//了解,字符串操作,把字符串中的hide替換成show。
// div.className = div.className.replace("show","hide");
}
}
</script>
</head>
<body>
<div class="nodeSmall" id="node_small">
<a href="#"></a>
<div class="erweima hide" id="er">
<img src="images/456.png" alt=""/>
</div>
</div>
</body>
</html>
02-禁用文本框
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
賬號(hào): <input type="text" value="傳智你好..."/><button>禁用</button><button>解禁</button><br><br>
密碼: <input type="password" value="aaabbbccc"/>
<script>
//需求:禁用文本框
//步驟:
//1.獲取事件源和相關(guān)元素
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序
//1.獲取事件源和相關(guān)元素
var inp = document.getElementsByTagName("input")[0];
var btn1 = document.getElementsByTagName("button")[0];
var btn2 = document.getElementsByTagName("button")[1];
//2.綁定事件
btn1.onclick = function () {
//3.書寫事件驅(qū)動(dòng)程序
inp.disabled = true;
}
btn2.onclick = function () {
//3.書寫事件驅(qū)動(dòng)程序
inp.disabled = false;
// inp.removeAttribute("disabled");
}
</script>
</body>
</html>
03-文本框獲取焦點(diǎn)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
input {
width: 300px;
height: 36px;
padding-left: 5px;
color: #ccc;
}
label {
position: absolute;
top: 82px;
left: 56px;
font-size: 12px;
color: #ccc;
cursor: text;
}
.hide {
display: none;
}
.show {
display: block;
}
</style>
</head>
<body>
京東:<input id="inp1" type="text" value="我是京東"/><br><br>
淘寶:<label for="inp2">我是淘寶</label><input id="inp2" type="text"/><br><br>
placeholder:<input type="text" placeholder="我是placeholder"/>
<script>
//需求:京東的獲取焦點(diǎn)。
//思路:京東的input按鈕獲取了插入條光標(biāo)立刻刪除內(nèi)容。失去插入條光標(biāo)顯示文字。
//步驟:
//1.獲取事件源和相關(guān)元素
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序
//1.獲取事件源和相關(guān)元素
var inp1 = document.getElementById("inp1");
//2.綁定事件(獲取焦點(diǎn)事件)
inp1.onfocus = function () {
//判斷,如果input里面的內(nèi)容是“我是京東”,那么把值賦值為“”;
if(this.value === "我是京東"){
// inp1.style.color = "#000";
inp1.value = "";
}
}
//(失去焦點(diǎn)事件)焦點(diǎn):插入條光標(biāo)
inp1.onblur = function () {
//失去焦點(diǎn)后判斷,如果input內(nèi)容為空,那么把內(nèi)容賦值為我是京東。
if(this.value === ""){
//3.書寫事件驅(qū)動(dòng)程序
// inp1.style.color = "#ccc";
inp1.value = "我是京東";
}
}
//需求:淘寶的獲取焦點(diǎn)。
//思路:在input中輸入文字,label標(biāo)簽隱藏,里面的文字變成空字符串,label顯示。
//步驟:
//1.獲取事件源和相關(guān)元素
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序
//1.獲取事件源和相關(guān)元素
var inp2 = document.getElementById("inp2");
var lab = document.getElementsByTagName("label")[0];
//2.綁定事件(輸入事件,文字的輸入和刪除都會(huì)觸動(dòng)這個(gè)事件)
//獲取插入條光標(biāo)
// inp2.focus();
inp2.oninput = function () {
//3.書寫事件驅(qū)動(dòng)程序
//判斷input中的值是否為空,如果為空,那么label顯示,否則隱藏。
if(this.value === ""){
lab.className = "show";
}else{
lab.className = "hide";
}
}
</script>
</body>
</html>
04-用戶注冊(cè)高亮顯示
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.wrong {
border: 2px solid red;
}
.right {
border: 2px solid #91B81D;
}
</style>
</head>
<body>
賬號(hào):<input type="text" onblur="fn(this)"/><br><br>
密碼:<input type="password" onblur="fn(this)"/>
<script>
//需求:失去焦點(diǎn)的時(shí)候判斷input按鈕中的值,如果賬號(hào)或密碼在6-12個(gè)字符之間通過(guò),否則報(bào)錯(cuò)。
//步驟:
//1.獲取事件源
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序
//3.書寫事件驅(qū)動(dòng)程序
function fn(aaa){
//html中的input標(biāo)簽行內(nèi)調(diào)用function的時(shí)候,是先通過(guò)window調(diào)用的function,所以打印this等于打印window
// console.log(this)
//只有傳遞的this才指的是標(biāo)簽本身。
// console.log(aaa)
// console.log(this.value)
if(aaa.value.length < 6 || aaa.value.length>12){
aaa.className = "wrong";
}else{
aaa.className = "right";
}
}
</script>
</body>
</html>
05-設(shè)置水果
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button>識(shí)別鯉魚(yú)</button><br><br>
<button>去掉</button><br><br>
<select id="sel" multiple>
<option value="0">香蕉</option>
<option value="1">蘋果</option>
<option value="2" selected="">鯉魚(yú)</option>
<option value="3">鴨梨</option>
</select>
<script>
//需求:點(diǎn)擊識(shí)別水產(chǎn),立刻把option對(duì)應(yīng)的鯉魚(yú)選擇的屬性設(shè)置為selected;
//步驟:
//1.獲取事件源和相關(guān)元素
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序
//1.獲取事件源和相關(guān)元素
var btn1 = document.getElementsByTagName("button")[0];
var btn2 = document.getElementsByTagName("button")[1];
var sel = document.getElementById("sel");
var optArr = sel.getElementsByTagName("option");
//2.綁定事件
btn1.onclick = function () {
//3.書寫事件驅(qū)動(dòng)程序
optArr[2].selected = true;
}
//
btn2.onclick = function () {
//3.書寫事件驅(qū)動(dòng)程序
optArr[2].selected = false;
optArr[3].selected = true;
}
</script>
</body>
</html>
06-for循環(huán)為文本框賦值和取值
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="text"/><br><br>
<input type="text"/><br><br>
<input type="text"/><br><br>
<input type="text"/><br><br>
<input type="text"/><br><br>
<input type="text"/><br><br>
<button>賦值</button><br><br>
<button>取值</button><br><br>
<script>
//for循環(huán)賦值
//老三步
var inpArr = document.getElementsByTagName("input");
var btnArr = document.getElementsByTagName("button");
//賦值
btnArr[0].onclick = function () {
//循環(huán)為每一個(gè)input賦值
for(var i=0;i<inpArr.length;i++){
inpArr[i].value = i;
}
}
//獲取值
btnArr[1].onclick = function () {
//循環(huán)獲取nput的值賦值為一個(gè)數(shù)組
var newArr = [];
for(var i=0;i<inpArr.length;i++){
newArr.push(inpArr[i].value);
}
console.log(newArr.join(" "));
}
</script>
</body>
</html>
07-全選反選
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="text"/><br><br>
<input type="text"/><br><br>
<input type="text"/><br><br>
<input type="text"/><br><br>
<input type="text"/><br><br>
<input type="text"/><br><br>
<button>賦值</button><br><br>
<button>取值</button><br><br>
<script>
//for循環(huán)賦值
//老三步
var inpArr = document.getElementsByTagName("input");
var btnArr = document.getElementsByTagName("button");
//賦值
btnArr[0].onclick = function () {
//循環(huán)為每一個(gè)input賦值
for(var i=0;i<inpArr.length;i++){
inpArr[i].value = i;
}
}
//獲取值
btnArr[1].onclick = function () {
//循環(huán)獲取nput的值賦值為一個(gè)數(shù)組
var newArr = [];
for(var i=0;i<inpArr.length;i++){
newArr.push(inpArr[i].value);
}
console.log(newArr.join(" "));
}
</script>
</body>
</html>
08-屬性的方法操作(getsetremoveAttribute)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="box" title="主體" class="asdfasdfadsfd">我愛(ài)你中國(guó)</div>
<script>
//兩種方式不能交換使用,賦值和獲取值必須使用用一種方法。
var div = document.getElementById("box");
//元素節(jié)點(diǎn).屬性(元素節(jié)點(diǎn)[屬性]):綁定的屬性值不會(huì)出現(xiàn)在標(biāo)簽上。
div.aaaa = "1111";
console.log(div.aaaa);
//get/set/removeAttribut: 綁定的屬性值會(huì)出現(xiàn)在標(biāo)簽上。
div.setAttribute("bbbb","2222");
console.log(div.getAttribute("aaaa"));
console.log(div.bbbb);
</script>
</body>
</html>
09-tab欄切換
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
padding: 0;
margin: 0;
}
.box {
width: 500px;
height: 400px;
border: 1px solid #ccc;
margin: 100px auto;
overflow: hidden;
}
ul {
width: 600px;
height: 40px;
margin-left: -1px;
list-style: none;
}
li {
float: left;
width: 101px;
height: 40px;
text-align: center;
font: 600 18px/40px "simsun";
background-color: pink;
cursor: pointer;
}
span {
display: none;
width: 500px;
height: 360px;
background-color: yellow;
text-align: center;
font: 700 150px/360px "simsun";
}
.show {
display: block;
}
.current {
background-color: yellow;
}
</style>
<script>
window.onload = function () {
//需求:鼠標(biāo)放到上面的li上,li本身變色(添加類),對(duì)應(yīng)的span也顯示出來(lái)(添加類);
//思路:1.點(diǎn)亮盒子。 2.利用索引值顯示盒子。
//步驟:
//1.獲取事件源和相關(guān)元素
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序(排他思想)
//1.獲取事件源和相關(guān)元素
var liArr = document.getElementsByTagName("li");
var spanArr = document.getElementsByTagName("span");
//2.綁定事件(循環(huán)綁定)
for(var i=0;i<liArr.length;i++){
//綁定索引值
liArr[i].index = i;
liArr[i].onmouseover = function () {
//3.書寫事件驅(qū)動(dòng)程序(排他思想)
//1.點(diǎn)亮盒子。 2.利用索引值顯示盒子。(排他思想)
for(var j=0;j<liArr.length;j++){
liArr[j].className = "";
spanArr[j].className = "";
}
this.className = "current";
spanArr[this.index].className = "show";
}
}
}
</script>
</head>
<body>
<div class="box">
<ul>
<li class="current">鞋子</li>
<li>襪子</li>
<li>帽子</li>
<li>褲子</li>
<li>裙子</li>
</ul>
<span class="show">鞋子</span>
<span>襪子</span>
<span>帽子</span>
<span>褲子</span>
<span>裙子</span>
</div>
</body>
</html>
tab欄切換(復(fù)雜版)
<script>
window.onload = function () {
//需求:鼠標(biāo)放到上面的li上,li本身變色(添加類),對(duì)應(yīng)的span也顯示出來(lái)(添加類);
//思路:1.點(diǎn)亮盒子。 2.利用索引值顯示盒子。
//步驟:
//1.獲取事件源和相關(guān)元素
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序(排他思想)
//1.獲取事件源和相關(guān)元素
var boxArr = document.getElementsByClassName("box");
//函數(shù)調(diào)用
for(var i=0;i<boxArr.length;i++){
fn(boxArr[i]);
}
//函數(shù)封裝
function fn(ele){
var liArr = ele.getElementsByTagName("li");
var spanArr = ele.getElementsByTagName("span");
//2.綁定事件(循環(huán)綁定)
for(var i=0;i<liArr.length;i++){
//綁定索引值(自定義屬性)
liArr[i].setAttribute("index",i);
liArr[i].onmouseover = function () {
//3.書寫事件驅(qū)動(dòng)程序(排他思想)
//1.點(diǎn)亮盒子。 2.利用索引值顯示盒子。(排他思想)
for(var j=0;j<liArr.length;j++){
liArr[j].removeAttribute("class");
spanArr[j].removeAttribute("class");
}
this.setAttribute("class","current");
spanArr[this.getAttribute("index")].setAttribute("class","show");
}
}
}
}
</script>
10-點(diǎn)亮盒子
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
button {
margin: 10px;
width: 100px;
height: 40px;
cursor: pointer;
}
.current {
background-color: yellow;
}
</style>
</head>
<body>
<button>按鈕1</button>
<button>按鈕2</button>
<button>按鈕3</button>
<button>按鈕4</button>
<button>按鈕5</button>
<script>
//需求:鼠標(biāo)放到哪個(gè)button上,改button變成黃色背景(添加類)
//步驟:
//1.獲取事件源
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序
//1.獲取事件源
var btnArr = document.getElementsByTagName("button");
//2.綁定事件
for(var i=0;i<btnArr.length;i++){
btnArr[i].onmouseover = function () {
//排他思想(干掉所有人,剩下我一個(gè))
//排他思想是和for循環(huán)連用
for(var j=0;j<btnArr.length;j++){
btnArr[j].className = "";
}
this.className = "current";
}
}
//3.書寫事件驅(qū)動(dòng)程序
</script>
</body>
</html>
11-彈出盒子的索引值
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
button {
margin: 10px;
width: 100px;
height: 40px;
cursor: pointer;
}
.current {
background-color: yellow;
}
</style>
</head>
<body>
<button>按鈕1</button>
<button>按鈕2</button>
<button>按鈕3</button>
<button>按鈕4</button>
<button>按鈕5</button>
<script>
//需求:鼠標(biāo)放到哪個(gè)button上,改button變成黃色背景(添加類)
//步驟:
//1.獲取事件源
//2.綁定事件
//3.書寫事件驅(qū)動(dòng)程序
//1.獲取事件源
var btnArr = document.getElementsByTagName("button");
//2.綁定事件
for(var i=0;i<btnArr.length;i++){
//每次循環(huán)綁定一個(gè)屬性,屬性值為該盒子的索引值
// btnArr[i].setAttribute("index",i);
btnArr[i].index = i;
btnArr[i].onmouseover = function () {
//排他思想(干掉所有人,剩下我一個(gè))
//排他思想是和for循環(huán)連用
for(var j=0;j<btnArr.length;j++){
btnArr[j].className = "";
}
this.className = "current";
// alert(this.getAttribute("index"));
alert(this.index);
}
}
//3.書寫事件驅(qū)動(dòng)程序
</script>
</body>
</html>
14-訪問(wèn)關(guān)系
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
li {
list-style: none;
width: 100px;
height: 100px;
background-color: pink;
margin: 5px;
}
</style>
</head>
<body>
<ul>
<li class="box1"></li>
<li class="box2"></li>
<li id="box3"></li>
<li class="box4"></li>
<li class="box5"></li>
</ul>
<script>
//parentNode父盒子
var box3 = document.getElementById("box3");
box3.parentNode.style.backgroundColor = "blue";
//兄弟節(jié)點(diǎn)(前一個(gè)和后一個(gè):previousSibling和nextSibling)
//previousElementSibling和nextElementSibling在IE678中不支持。IE678不能獲取文本節(jié)點(diǎn)。
// box3.previousElementSibling.style.backgroundColor = "red";
// box3.nextElementSibling.style.backgroundColor = "yellow";
// box3.previousSibling.style.backgroundColor = "red";
// box3.nextSibling.style.backgroundColor = "yellow";
var pre = box3.previousElementSibling || box3.previousSibling;
var net = box3.nextElementSibling || box3.nextSibling;
pre.style.backgroundColor = "red";
net.style.backgroundColor = "yellow";
//單個(gè)子元素(firstChild和lastChild)
// box3.parentNode.firstElementChild.style.backgroundColor = "orange";
// box3.parentNode.lastElementChild.style.backgroundColor = "green";
var first = box3.parentNode.firstElementChild || box3.parentNode.firstChild;
var last = box3.parentNode.lastElementChild || box3.parentNode.lastChild;
first.style.backgroundColor = "orange";
last.style.backgroundColor = "green";
//所有子元素
var arr = box3.parentNode.children;
for(var i=0;i<arr.length;i++){
arr[i].style.backgroundColor = "black";
}
console.log(box3.parentNode.childNodes);
var arr2 = box3.parentNode.childNodes;
for(var i=0;i<arr2.length;i++){
if(arr2[i].nodeType === 1){
console.log(arr2[i]);
}
}
//隨意獲取指定兄弟節(jié)點(diǎn)
var index = 0;
var node = box3.parentNode.children[index];
//獲取所有的兄弟節(jié)點(diǎn)
function siblings(elm) {
var a = [];
var p = elm.parentNode.children;
for(var i =0;i<p.length;i++) {
if(p[i] !== elm) {
a.push(p[i]);
}
}
return a;
}
</script>
</body>
</html>
15-nodeType和nodeName和nodeValue
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="box" value="111">你好</div>
<script>
var ele = document.getElementById("box");//元素節(jié)點(diǎn)
var att = ele.getAttributeNode("id");//屬性節(jié)點(diǎn)
var txt = ele.firstChild;
// console.log(ele);
// console.log(att);
// console.log(txt);
//nodeType
console.log(ele.nodeType);//1
console.log(att.nodeType);//2
console.log(txt.nodeType);//3
//nodeName
console.log(ele.nodeName);//DIV
console.log(att.nodeName);//id
console.log(txt.nodeName);//#text
//nodeValue
console.log(ele.nodeValue);//null
console.log(att.nodeValue);//box
console.log(txt.nodeValue);//你好
</script>
</body>
</html>
16-隔行變色
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
ul {
width: 1210px;
margin: 100px auto;
}
li {
height: 34px;
cursor: pointer;
font: 500 16px/34px "simsun";
}
</style>
</head>
<body>
<ul>
<li>北京股指 3063.31-22.18 (-0.72%)</li>
<li>上海股指 3063.31-22.18 (-0.72%)</li>
<li>廣州股指 3063.31-22.18 (-0.72%)</li>
<li>深圳股指 3063.31-22.18 (-0.72%)</li>
<li>北京股指 3063.31-22.18 (-0.72%)</li>
<li>上海股指 3063.31-22.18 (-0.72%)</li>
<li>廣州股指 3063.31-22.18 (-0.72%)</li>
<li>深圳股指 3063.31-22.18 (-0.72%)</li>
</ul>
<script>
//需求:利用childNodes實(shí)現(xiàn)各行變色
//簡(jiǎn)單版
// var arr = document.getElementsByTagName("li");
// for(var i=0;i<arr.length;i++){
// if(i%2===0){
// arr[i].style.backgroundColor = "#ccc";
// }
// }
//復(fù)雜版
//獲取ul
var ul = document.getElementsByTagName("ul")[0];
var arr = ul.childNodes;
//把元素節(jié)點(diǎn)重新放入一個(gè)新數(shù)組
var newArr = [];
for(var i=0;i<arr.length;i++){
if(arr[i].nodeType === 1){
newArr.push(arr[i]);
}
}
//隔行變色
for(var i=0;i<newArr.length;i++){
if(i%2!=0){
newArr[i].style.backgroundColor = "red";
}
}
</script>
</body>
</html>