<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> //可以构造出一种选择器 <input type="button" value="点我啊" /> <div> <div>123</div> <div alex='sb'>123</div> <div alex='sb'>123</div> <div alex='sb'>123</div> <div>123</div> <div alex='sb'>123</div> <div>123</div> <div alex='sb'>123</div> <div>123</div> </div> <script> function Func(){ //i1所有孩子,循环每一个孩子,判断如果alex='sb' var i1 = document.getElementById('i1') var divs = i1.children for ( var i =0; i<divs.length;i++){ var current_div = divs[i]; var result = current_div.getAttribute('alex')//得到属性 //console.log(result); if (result == 'sb'){ current_div.innerText = '456'; } } } </script> </body> </html>
案例五:获取输入框内的值三种方法