這篇文章將為大家詳細(xì)講解有關(guān)JS中如何使用選擇器及屬性、方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)于2013年開始,先為武威等服務(wù)建站,武威等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為武威企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
選擇器、屬性及方法調(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" ></div>
<div class="b" ></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>關(guān)于“JS中如何使用選擇器及屬性、方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
新聞名稱:JS中如何使用選擇器及屬性、方法
文章出自:http://www.chinadenli.net/article22/pesgjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、搜索引擎優(yōu)化、App設(shè)計、網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計公司、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)