📌  相关文章
📜  如何使用jQuery按名称选择元素?

📅  最后修改于: 2022-05-13 01:55:54.933000             🧑  作者: Mango

如何使用jQuery按名称选择元素?

可以使用 2 种方法通过 name 属性选择元素:

方法一:使用名称选择器方法

name 属性选择器可用于按名称选择元素。此选择器选择值与指定值完全相等的元素。

句法:

[name="nameOfElement"]

例子:





  

    How to select an element by name with jQuery?

  

    
        

GeeksforGeeks

        How to select an element by name with jQuery?         

            The textbox below has the name attribute 'address'.             

                             
        

        

            The textbox below has the name attribute 'area'.             

                             
        

        

Click on the button to hide the input with             the name 'area'

                               
  

输出:

  • 点击按钮前:
    选择器之前
  • 点击按钮后:
    选择器后

方法 2:使用 JavaScript 按名称获取元素并将其传递给 jQuery

JavaScript 方法 getElementsByName() 可用于选择所需元素,并可将其传递给 jQuery函数以进一步将其用作 jQuery 对象。

句法:

selector = document.getElementsByName('nameOfElement');
element = $(selector);

例子:



  

    How to select an element by name with jQuery?

  

    
        

GeeksforGeeks

        How to select an element by name with jQuery?         

            The textbox below has the name attribute 'address'.             

                             
        

        

            The textbox below has the name attribute 'area'.             

                             
        

        

Click on the button to hide the              input with the name 'address'

                               
  

输出:

  • 点击按钮前:
    正常 js 之前
  • 点击按钮后:
    普通 js 之后

jQuery 是一个开源 JavaScript 库,它简化了 HTML/CSS 文档之间的交互,它以其“少写,多做”的理念而广为人知。
您可以按照此 jQuery 教程和 jQuery 示例从头开始学习 jQuery。