代码之家  ›  专栏  ›  技术社区  ›  tmighty

如何向后运行querySelector?

  •  0
  • tmighty  · 技术社区  · 3 年前

    在文档中,有两个元素的类名为“div.\u 13NKt.copyable text.selective text”。 (网址:whatsapp.com)

    一个在文档顶部,一个在文档底部。

    我想查询选择第二个元素/底部的元素。

    我该怎么做?

    是否有向后执行查询选择的选项?

    以下是我目前的代码:

    document.querySelector("div._13NKt.copyable-text.selectable-text").focus();
    

    我正在使用基于Chromium的mobileFX web浏览器运行此命令。

    我这样称呼它:

    webkit1.Eval("document.querySelector(" & Quote("div._13NKt.copyable-text.selectable-text") & ").focus();")
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   zbyso    3 年前

    像这样的?

    const elements = document.querySelectorAll("div._13NKt.copyable-text.selectable-text");
    if(elements.length) elements[elements.length - 1].focus();
    
        2
  •  1
  •   Mateo Guio    3 年前

    我想你可以使用文档。查询Selectorall(“div.\u 13NKt.可复制文本.可选文本”),然后访问索引。

    //Capture the elements
    let myVar = document.querySelectorAll("div._13NKt.copyable-text.selectable-text");
    
    //Then access to the index, taking into account that the initial index is 0.
    console.log(myVar[1])
    //Or
    var secObject = myVar[1];