【问题标题】:document.querySelector for name as css selector?名称为css选择器的document.querySelector?
【发布时间】:2017-04-05 10:33:50
【问题描述】:

我知道 document.querySelector('..') 将 css 选择器作为参数,例如

a.className、#idName 等......

但我不知道如何使用名称。 我看到了一个例子,但看不懂。

document.querySelector('[name="billContact.email"]')

【问题讨论】:

  • 什么是 billContact,什么是电子邮件。 email 是一个类,而 billContact 是一个 name 属性吗?
  • "billContact.email"是属性name的值
  • 这个可能的 div 语法是什么?
  • 当您发布问题时,请确保您在描述中发布所有说明,而不是在 cmets 中提问。并发布您坚持使用的代码或示例。没有相关代码的单行疑问不是提问的方式。请说清楚。

标签: jquery html


【解决方案1】:

您可以使用 document.querySelectorAll 通过属性查找元素。

document.querySelectorAll('[name="test"]');

【讨论】:

  • 你能告诉一个 div 语法,document.querySelector('[name="billContact.email"]') 将在哪里访问 billContact.email 选择器。
【解决方案2】:

意思是“name='billContact.email'的一切”

P.S 查看this 了解更多信息

【讨论】:

    【解决方案3】:

    TL;DR

    [HTML-Node-Attribute=""]


    属性选择器

    HTML-Element-Attributes idclass 确实有自己的 css-selecoring:

    • #id
    • .class

    其他属性确实需要其他东西。比如“属性选择器”。

    例如,您有以下类似的 HTML 代码:

    <a href="#top">Go to top!</a>
    

    使用属性选择器,您可以获取此元素,并具有以下所有可能性:

    [href] /*looking only for the attribut. No matter what value is inside*/
    [href="#top"] /*the attributes value must be "#top"*/
    a[href="#top"] /*the same as the example above but in this case href needs to be a attribute of "a"-tag*/
    

    有一些更酷的可能性来处理一个元素。更多信息见MDN Attribute selectors

    querySelector / querySelectorAll

    querySelector 为您提供等于传递的第一个元素 CSS 选择器。

    好的,我想这可能更容易通过示例而不是文字墙来理解。那么让我们开始吧:

    对于这个例子,我将使用这个 HTML 片段:

    <div class="container">
        <div data-id='cat'>
            <button name='cat' value='miau'>Cat</button>
        </div>
        <div data-id='dog'>
            <button name='dog' value='not miau'>Dog</button>
        </div>
    </div>
    

    当您询问如何通过 querySelector 的名称查找元素时,我将从以下内容开始:

    有两个具有名称属性的元素。两者都是按钮,但名称不同。

    要获取具有 name 属性的第一个元素,请使用:

    document.querySelector('[name]') //get button cat because it's the first one in the dom (in that example)
    

    要让他们都试试这个:

    document.querySelectorAll('[name]') //get both buttons (cat, dog)
    

    当你知道你要找的名字时

    document.querySelector('[name="cat"]') //get the first element where name equals 'cat'
    

    与数据、类或 id 等其他属性相同

    document.querySelector('[data-id="cat"]') //get div
    

    您还可以一次执行多个 dom 步骤:

    document.querySelector('[data-id="cat"] button') //gets the first button inside "cat-div"
    

    文档

    请参阅MDNMicrosoft 了解更多信息。 您可能也对CSS-Selectors 感兴趣。

    【讨论】:

    • 长答案,解释querySelector 的工作原理,但不是针对实际问题。 OP 表示他知道 querySelector 是如何工作的 :)
    • @Martijn 哎呀,你是对的。我会在几分钟后编辑我的答案。
    【解决方案4】:

    对于 document.querySelector('[name="billContact.email"]')
    您需要的输入标签如下;

    你可以试试('input[name="billContact.email"]'):

    function checkForm(){
     var form = document.forms[0];
     var selectElement = form.querySelector('input[name="billContact.email"]');
     var selectedValue = selectElement.value;
    

    }

    下次咨询https://developer.mozilla.org

    这是我的小提琴http://jsfiddle.net/2ZL4G/167/

    【讨论】:

    • 这不能回答问题。 OP 想了解原因,这只是展示 OP 最初是如何好奇的一个例子。
    猜你喜欢
    • 2017-09-22
    • 2016-09-11
    • 2011-07-06
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 2023-02-15
    相关资源
    最近更新 更多