淺談JS中的常用選擇器及屬性、方法的調(diào)用
更新時間:2017年07月28日 07:49:10 投稿:jingxian
下面小編就為大家?guī)硪黄獪\談JS中的常用選擇器及屬性、方法的調(diào)用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
選擇器、屬性及方法調(diào)用的配合使用:
<style> #a{ width: 200px; height: 100px; background-color: red; } .b{ width: 200px; height: 100px; background-color: green; } .div1{ width: 200px; height: 100px; background-color:aqua; } </style> <body> <div id="a"></div> <div class="b" style="background-color: black;"></div> <div class="b" style="background-color: chartreuse;"></div> <div class="div1">精英教育</div> <input type="text" name="ipt1" /> <input type="checkbox" name="ckb2" / disabled="disabled"> </body> <script> //先選擇元素在進行加效果 //ID選擇器(使用較高JS) var a = document.getElementById("a"); //檢測類型 alert(typeof(document.getElementById("a"))) a.style.backgroundColor="royalblue"; a.innerHTML ="<span>hello<span>"; a.innerText = "<span>hello<span>"; // class選擇器 var b_class = document.getElementsByClassName("b"); b_class[0].style.backgroundColor="red"; var b_class = document.getElementsByClassName("b"); b_class[1].style.backgroundColor="blueviolet"; // 標(biāo)簽選擇器 var b_b = document.getElementsByTagName("div"); b_b[1].style.backgroundColor="yellow"; var div_1 = document.getElementsByName("ipt1"); div_1[0].value="文本框"; var ckb2 = document.getElementsByName("ckb2")[0]; ckb2.setAttribute("checked",""); //移除屬性 ckb2.removeAttribute("disabled") //創(chuàng)造元素 var a = document.createElement("a"); a.setAttribute("href","http://www.baidu.com"); a.innerText="百度一下"; document.body.appendChild(a); document.body.removeChild(a); div1.appendChild(a); </script> <body> <!--DOM Document Object Model BOM Bowers O M--> <div id="div1" class="div1"></div> <div class="div1"></div> <input type="text" name="ipt1" /> <input type="checkbox" name="ckb1" disabled="disabled"/> </body> </html> <script> // alert('1111'); // window.alert('123') var div1 = document.getElementById('div1'); div1.style.backgroundColor = 'red'; // div1.innerHTML = "<ul><li>123456</li></ul>"; div1.innerText = "<ul><li>123456</li></ul>"; var div1_class = document.getElementsByClassName('div1'); div1_class[1].style.backgroundColor = "green"; var div1_1 = document.getElementsByTagName('div'); div1_1[1].style.backgroundColor = "blue"; // jQuery var div1_2 = document.getElementsByName('ipt1'); div1_2[0].value = '123'; var ckb1 = document.getElementsByName('ckb1')[0]; // ckb1.setAttribute("checked","checked"); ckb1.removeAttribute('disabled'); var a = document.createElement("a"); a.setAttribute("href","http://www.baidu.com"); a.innerText = "百度"; document.body.appendChild(a); document.body.removeChild(a); div1.appendChild(a); </script>
以上這篇淺談JS中的常用選擇器及屬性、方法的調(diào)用就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JS實現(xiàn)超簡單的漢字轉(zhuǎn)拼音功能示例
這篇文章主要介紹了JS實現(xiàn)超簡單的漢字轉(zhuǎn)拼音功能,結(jié)合實例形式分析了javascript漢字轉(zhuǎn)換成拼音的函數(shù)定義與使用技巧,需要的朋友可以參考下2016-12-12幫你徹底搞懂JS中的prototype、__proto__與constructor(圖解)
這篇文章主要介紹了詳解幫你徹底搞懂JS中的prototype、__proto__與constructor(圖解),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08js遍歷json對象所有key及根據(jù)動態(tài)key獲取值的方法(必看)
下面小編就為大家?guī)硪黄猨s遍歷json對象所有key及根據(jù)動態(tài)key獲取值的方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03bootstrap動態(tài)添加面包屑(breadcrumb)及其響應(yīng)事件的方法
這篇文章主要介紹了bootstrap動態(tài)添加面包屑(breadcrumb)及其響應(yīng)事件的方法,涉及js數(shù)據(jù)傳輸及定義響應(yīng)事件相關(guān)操作技巧,需要的朋友可以參考下2017-05-05