当前位置: 代码网 > it编程>编程语言>Javascript > JavaScript中的document.querySelector()方法使用详解

JavaScript中的document.querySelector()方法使用详解

2024年07月05日 Javascript 我要评论
1. document.queryselectordocument.queryselector可以获取文档中的第一个匹配的元素。这个函数返回匹配的元素(如果找到了匹配项),否则返回 null1.1 语

1. document.queryselector

document.queryselector 可以获取文档中的第一个匹配的元素。
这个函数返回匹配的元素(如果找到了匹配项),否则返回 null

1.1 语法:

const element = document.queryselector(selector);

1.2 示例

以下示例, 包含根据标签名选择,类名选择,属性名等选择元素

<!doctype html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>node-link tree</title>
  </head>

  <body>
    <div class="container">
      <h1>你好,2024!</h1>
      <h2>你好.冬天</h2>
      <p>龙马精神</p>
      <span class="first">龙年大吉</span>
      <p class="first">龙凤呈祥</p>
      <br />
      <a target="self">1.龙年行大运,祥瑞照门庭。岁月悠悠,情谊绵长,感恩有您相伴。愿您在新的一年里,如龙腾飞,事业兴旺;如龙入海,生活美满。祝您龙年吉祥如意!
      </a>
      <br>
      <a target="_blank">龙年到来,祥瑞满天。愿您如龙般矫健,事业腾飞;如龙鳞闪耀,生活美满。祝您龙年大吉,万事如意!
      </a>
    </div>
    <script>
      // 标签名为 p 的第一个元素
      document.queryselector("p").style.color = "red";
      // class="first" 的第一个元素:
      document.queryselector(".first").style.color = "green";
      //签名为 p,且class="first" 的第一个元素
      document.queryselector("p.first").style.background = "pink";

      //带target属性的第一个a元素
      document.queryselector("a[target]").style.background = "skyblue";
      //target属性为_blank的第一个a元素
      document.queryselector("a[target='_blank']").style.background = "skyblue";

      //多元素选择时,根据文档,哪个在前面就先匹配哪个
      document.queryselector("h1,h2").style.backgroundcolor = "yellow";
    </script>
  </body>
</html>

2.queryselectorall()

当页面中出现多个相同class标签(或者多个相同标签(比如多个div))时,如果你需要返回所有的元素,请使用 queryselectorall() 方法替代。

该方法返回所有满足条件的元素,结果是个nodelist集合。查找规则与前面所述一样。 ---->queryselectorall 得到一个伪数组 dom。

parentnode.queryselectorall()的结果是nodelist数组,但是又不是一般的js数组(array),所以也不能直接调用array的方法

<!doctype html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>node-link tree</title>
  </head>

  <body>
    <div class="container">
      <p>龙马精神</p>
      <p class="first">龙凤呈祥</p>
    </div>
    <script>
    
      // 标签名为 p 的第一个元素
      var pall = document.queryselectorall("p")
      pall[0].style.color = 'pink'
      console.log(pall)
      
    </script>
  </body>
</html>

3.document.queryselector.bind和document.queryselectorall.bind

定义全局的变量,方便直接获取dom

<!doctype html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>node-link tree</title>
  </head>

  <body>
    <div class="container">
      <p>龙马精神</p>
      <span class="first">龙年大吉</span>
      <p class="first">龙凤呈祥</p>
    </div>
    <script>
        var query = document.queryselector.bind(document);
        var query_tagname = query('p')
        console.log(query_tagname)
        query_tagname.style.color = 'red'
    </script>
  </body>
</html>

附:使用document.queryselector()搜索时,点‘.‘和井号‘#‘的用法

document.queryselector()用于选择文档中的元素,你可以根据元素的类型(标签名div/span)、类名(class)、或 id 来进行选择。在使用时,可以根据以下规则来确定何时使用点 (`.`) 和何时使用井号 (`#`):

1. 使用点 (`.`):

当你希望选择具有特定类名的元素时,使用点。

例如,`document.queryselector('.classname')` 会选择具有指定类名的元素。

例如,<div class="box"></div> ,你可以使用 `document.queryselector('.box')` 来选择它。

2. 使用井号 (`#`):

当你希望选择具有特定 id 的元素时,使用井号。

例如,`document.queryselector('#elementid')` 会选择具有指定 id 的元素。

例如, <div id="mydiv"></div>,你可以使用 `document.queryselector('#mydiv')` 来选择它。

3. 使用标签名:

如果你想选择特定类型的元素,只需提供标签名即可。

例如,`document.queryselector('div')` 会选择页面上的第一个 `<div>` 元素。

这不需要点 (`.`) 或井号 (`#`) 前缀。

总之,前缀点 (`.`) 用于选择类名,前缀井号 (`#`) 用于选择 id,而不使用前缀时用于选择标签名。根据需要选择适当的前缀,以便准确地选择目标元素。

总结 

到此这篇关于javascript中document.queryselector()方法使用详解的文章就介绍到这了,更多相关js document.queryselector()方法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com