国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

前端基礎(chǔ)(七)_DOM元素獲取(getElementById、getElementsByTagName、getElementsByClassName、querySelector等)

這篇具有很好參考價值的文章主要介紹了前端基礎(chǔ)(七)_DOM元素獲取(getElementById、getElementsByTagName、getElementsByClassName、querySelector等)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、通過id獲取元素(一次一個元素)

一個id在一個頁面是唯一的,所以我們可以使用

document.getElementById("id名")

獲取元素,使用變量來接收,直接就能獲取到這個元素,通過 id 名去獲取元素,一次只能獲取一個元素。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LiuQing</title>
  <style>
    #box {
      background-color: red;
    }
  </style>
</head>

<body>
  <div id="box">LiuQing</div>
  <script>
    var box = document.getElementById('box')
    console.log(box)
  </script>
</body>

</html>

getelementbyid,# 前端基礎(chǔ)(JS),前端,javascript,獲取元素
直接能獲取到這個元素。

二、通過標(biāo)簽名元素(一次多個元素)

通過標(biāo)簽名獲?。?/p>

document.getElementsByTagName("標(biāo)簽名");

例子:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LiuQing</title>
  <style>
    #box {
      background-color: red;
    }
  </style>
</head>

<body>
  <div id="box">LiuQing</div>
  <div class="Liuqing">Liuqing1</div>
  <div class="Liuqing">Liuqing2</div>
  <script>
    var box = document.getElementById('box')
    console.log(box)
    var Liuqing = document.getElementsByTagName('div')
    console.log(Liuqing, 'Liuqing')
    console.log(Liuqing[0], 'Liuqing[0]')
  </script>
</body>

</html>

getelementbyid,# 前端基礎(chǔ)(JS),前端,javascript,獲取元素

通過

 var Liuqing = document.getElementsByTagName('div')

獲取所有div標(biāo)簽元素,得到的是一個HTMLCollection數(shù)組,元素集合,然后去集合的第0項,也就是id名為box的元素。

三、通過類名元素(一次多個元素)

通過類名獲取:

document.getElementsByClassName("類名");

例子:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LiuQing</title>
  <style>
    #box {
      background-color: red;
    }
  </style>
</head>

<body>
  <div id="box">LiuQing</div>
  <div class="Liuqing">Liuqing1</div>
  <div class="Liuqing">Liuqing2</div>
  <script>
    var box = document.getElementById('box')
    console.log(box)
    var Liuqing = document.getElementsByClassName('Liuqing')
    console.log(Liuqing, 'Liuqing')
    console.log(Liuqing[0], 'Liuqing[0]')
  </script>
</body>

</html>

getelementbyid,# 前端基礎(chǔ)(JS),前端,javascript,獲取元素
通過

 var Liuqing = document.getElementsByClassName('Liuqing')

獲取所有class名稱為Liuqing的標(biāo)簽元素,得到的是一個HTMLCollection數(shù)組,元素集合,然后去集合的第0項,也就是第一個calss名稱為Liuqing的元素。

四、querySelector(獲取第一個匹配成功的元素)

用法:

document.querySelector('類名(需要加.)|| id(需要加 #)||標(biāo)簽名')

注意:
獲取第一個匹配成功的元素

例子:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LiuQing</title>
  <style>
    #box {
      background-color: red;
    }
  </style>
</head>

<body>
  <div id="box">LiuQing</div>
  <div class="Liuqing">Liuqing1</div>
  <div class="Liuqing Liuqing2">Liuqing2</div>
  <script>
    var box = document.getElementById('box')
    console.log(box)
    var Liuqing1 = document.querySelector('.Liuqing')
    console.log(Liuqing1, 'Liuqing1')
    var Liuqing2 = document.querySelector('.Liuqing2')
    console.log(Liuqing2, 'Liuqing2')

  </script>
</body>

</html>

getelementbyid,# 前端基礎(chǔ)(JS),前端,javascript,獲取元素

五、querySelectorAll(獲取所有匹配成功的元素)

用法:

document.querySelector('類名(需要加.)|| id(需要加 #)||標(biāo)簽名')

注意:
獲取所有匹配成功的元素

例子:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LiuQing</title>
  <style>
    #box {
      background-color: red;
    }
  </style>
</head>

<body>
  <div id="box" class="Liuqing">
    <span class="smallSpan">
      span內(nèi)容
    </span>
  </div>
  <div class="Liuqing">
    <span class="smallSpan">
      span內(nèi)容
    </span>
  </div>
  <script>
    var Liuqing = document.querySelectorAll('.Liuqing .smallSpan')
    console.log(Liuqing, 'Liuqing')

  </script>
</body>

</html>

getelementbyid,# 前端基礎(chǔ)(JS),前端,javascript,獲取元素
document.querySelectorAll中可以寫選擇樣式,css中怎么寫,這里就可以怎么寫。文章來源地址http://www.zghlxwxcb.cn/news/detail-789197.html

到了這里,關(guān)于前端基礎(chǔ)(七)_DOM元素獲?。╣etElementById、getElementsByTagName、getElementsByClassName、querySelector等)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 2.獲取DOM元素

    2.獲取DOM元素

    獲取DOM元素就是利用JS選擇頁面中的標(biāo)簽元素 語法: 參數(shù): 包含一個或多個有效的CSS選擇器 字符串 返回值: CSS選擇器匹配的第一個元素,一個HTMLElement對象。 如果沒有匹配到,則返回null。 語法: 參數(shù): 包含一個或多個有效的CSS選擇器 字符串 返回值: CSS選擇器匹配的 NodeList 對象

    2024年02月15日
    瀏覽(19)
  • javaScript:DOM元素的獲?。o態(tài)/動態(tài)獲取)
  • JavaScript---獲取、修改DOM元素

    1. 獲取元素 1.1.? 獲取第一個元素,一個HTMLElement對象,可以修改 ? document.querySelector(\\\'css選擇器\\\')? ? 1.2. 獲取多個元素,一個HTMLElement對象,不可以修改,得到的是偽數(shù)組 document.querySelectorAll(\\\'css選擇器\\\') ? 2.?操作元素內(nèi)容 2.1. 元素.innerText? ? ? 只顯示純文本,不解析標(biāo)簽?

    2024年02月15日
    瀏覽(21)
  • uniapp獲取dom某個元素

    uniapp獲取dom某個元素

    uniapp中想要獲取dom元素不能像pc端那樣獲取 雖然dom元素是獲取到了,但不同于pc端那樣,獲取到的元素屬性和方法少很多: ?只有這么幾個屬性,click點擊方法都沒有,可能是考慮到多端適配的問題

    2024年02月13日
    瀏覽(21)
  • 微信小程序獲取dom元素

    微信小程序獲取dom元素

    微信小程序不支持document.querySelect獲取元素,它內(nèi)置了獲取元素的兩種方法,第一種是通過 wx.createSelectorQuery()獲取dom元素,第二種時給想要使用的對象綁定事件,輸出e對象,就能拿到該對象的一些信息 先在頁面上定義一個view標(biāo)簽 ?

    2024年02月08日
    瀏覽(24)
  • 「JavaScript DOM編程必備」元素獲取方法大全

    ????????在編寫 JavaScript 代碼時,我們經(jīng)常需要獲取 HTML 文檔中的元素。下面將介紹 JavaScript 中獲取元素的幾種基本方法。 要通過元素的 ID 獲取該元素對象,可以使用 document.getElementById() 方法。該方法接收一個字符串參數(shù),即元素的 ID。例如,如果要獲取 ID 為 \\\"example\\\" 的

    2024年02月07日
    瀏覽(26)
  • JS中獲取dom元素高度相關(guān)方法

    JS中獲取dom元素高度相關(guān)方法

    javascript中獲取dom元素高度和寬度的屬性如下: 網(wǎng)頁可視區(qū)域?qū)挘?document.body.clientWidth 網(wǎng)頁可視區(qū)域高: document.body.clientHeight 網(wǎng)頁可視區(qū)域?qū)挘?document.body.offsetWidth (包括邊距的寬) 網(wǎng)頁可視區(qū)域高: document.body.offsetHeight (包括邊距的高) 網(wǎng)頁正文全文寬: document.body.scrollWidth

    2024年02月07日
    瀏覽(21)
  • JS 獲取 HTML DOM 元素的方法

    JS 獲取 HTML DOM 元素的方法

    ?

    2024年02月11日
    瀏覽(22)
  • 小程序獲取窗口寬高和dom元素

    wx.getSystemInfoSync().windowWidth // 獲取當(dāng)前窗口的寬度 wx.getSystemInfoSync().windowHeight // 獲取當(dāng)前窗口的高度 wx.getSystemInfoSync().model // 獲取當(dāng)前采用的設(shè)備 wx.getSystemInfoSync().pixelRatio wx.getSystemInfoSync().language // 獲取當(dāng)前所采用的的語言 wx.getSystemInfoSync().version // 獲取當(dāng)前設(shè)備的版本 ==

    2024年02月15日
    瀏覽(29)
  • js獲取dom元素寬高的方法

    js獲取dom元素寬高的方法

    這種?法,有?定局限性,只能取 內(nèi)聯(lián)樣式 的寬?。 這種?法,也是有?定局限性,不過我們?nèi)N常?css樣式都能獲取。但是 只?持 IE ,其它瀏覽器不?持 ?持所有瀏覽器,兼容性好 這種?法,?般?于計算元素的絕對位置,根據(jù)視窗左上?的點來算的??梢阅玫剿膫€元

    2024年02月15日
    瀏覽(16)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包