I am working with the following svg Svg

const selector = document.querySelector('.yAxis g:nth-child(3)');
<!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>Document</title>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720">
  <rect class="vBoxRect" width="1280" height="720" fill="none" stroke="red"></rect>
  <rect class="boundRect" x="50" y="50" width="1180" height="620" fill="none" stroke="green"></rect>
  <g class="bound" style="transform: translate(50px, 50px);">
    <g fill="none" font-size="10" font-family="sans-serif" text-anchor="end" class="yAxis">
      <path class="domain" stroke="currentColor" d="M0.5,620.5V0.5"></path>
      <g class="tick" opacity="1" transform="translate(0,620.5)">
        <line stroke="currentColor" x2="-6"></line>
        <text fill="currentColor" x="-9" dy="0.32em">0</text>
      </g>
      <g class="tick" opacity="1" transform="translate(0,549.531741126106)">
        <line stroke="currentColor" x2="-6"></line>
        <text fill="currentColor" x="-9" dy="0.32em">10,000</text>
      </g>
      <g class="tick" opacity="1" transform="translate(0,478.56348225221205)">
        <line stroke="currentColor" x2="-6"></line>
        <text fill="currentColor" x="-9" dy="0.32em">20,000</text>
      </g>
      <g class="tick" opacity="1" transform="translate(0,407.59522337831805)">
        <line stroke="currentColor" x2="-6"></line>
        <text fill="currentColor" x="-9" dy="0.32em">30,000</text>
      </g>
      
    </g>
  </g>
</svg>
</body>
</html>

我想写一个 Select 2nd <g> element of class="yAxis"的查询

<g class="tick" opacity="1" transform="translate(0,549.531741126106)">
    <line stroke="currentColor" x2="-6"></line>
    <text fill="currentColor" x="-9" dy="0.32em">10,000</text>
  </g>

const selector = document.querySelector('.yAxis g:nth-child(3)');是 Select 正确的元素,但为什么我在 Select class=yAxis2nd g元素时要将3作为nth-child(3)值传递

推荐答案

您应该使用:nth-of-type而不是:nth-child,因为:nth-child Select 器还将计算第一个path元素:

const selector = document.querySelector('.yAxis g:nth-of-type(2)');
console.log(selector.outerHTML);
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1280 720">
  <rect class="vBoxRect" width="1280" height="720" fill="none" stroke="red"></rect>
  <rect class="boundRect" x="50" y="50" width="1180" height="620" fill="none" stroke="green"></rect>
  <g class="bound" style="transform: translate(50px, 50px);">
    <g fill="none" font-size="10" font-family="sans-serif" text-anchor="end" class="yAxis">
      <path class="domain" stroke="currentColor" d="M0.5,620.5V0.5"></path>
      <g class="tick" opacity="1" transform="translate(0,620.5)">
        <line stroke="currentColor" x2="-6"></line>
        <text fill="currentColor" x="-9" dy="0.32em">0</text>
      </g>
      <g class="tick" opacity="1" transform="translate(0,549.531741126106)">
        <line stroke="currentColor" x2="-6"></line>
        <text fill="currentColor" x="-9" dy="0.32em">10,000</text>
      </g>
      <g class="tick" opacity="1" transform="translate(0,478.56348225221205)">
        <line stroke="currentColor" x2="-6"></line>
        <text fill="currentColor" x="-9" dy="0.32em">20,000</text>
      </g>
      <g class="tick" opacity="1" transform="translate(0,407.59522337831805)">
        <line stroke="currentColor" x2="-6"></line>
        <text fill="currentColor" x="-9" dy="0.32em">30,000</text>
      </g>
    </g>
  </g>
</svg>

Javascript相关问答推荐

jQuery提交按钮重新加载页面,即使在WordPress中使用preventDefault()

Angular material 拖放堆叠的牌副,悬停时自动展开&

如何在模块层面提供服务?

保持物品顺序的可变大小物品分配到平衡组的算法

我怎么在JS里连续加2个骰子的和呢?

使用Google API无法进行Web抓取

变量在导入到Vite中的另一个js文件时成为常量.

如何访问此数组中的值?

Next.js中的服务器端组件列表筛选

FileReader()不能处理Firefox和GiB文件

当我点击一个按钮后按回车键时,如何阻止它再次被点击

JavaScript -复制到剪贴板在Windows计算机上无效

bootstrap S JS赢得了REACT中的函数/加载

我们是否可以在reactjs中创建多个同名的路由

无法在Adyen自定义卡安全字段创建中使用自定义占位符

当一条路由在Reaction路由中命中时,如何有条件地渲染两个组件或更改两个插座?

JavaScript -如何跳过某个字符(S)来打乱字符串中的字符

正在发出错误的URL请求

如何根据获取的对象数量动态生成状态变量

在Java脚本/类型脚本中覆盖父构造函数中的默认值