jQuery 如何使用jquery按名称获取元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7248815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:35:50  来源:igfitidea点击:

how to get elements by name using jquery?

jqueryelements

提问by sahar

how to get html element by name using jquery without using id or class ? Is there any way to do that?

如何在不使用 id 或 class 的情况下使用 jquery 按名称获取 html 元素?有没有办法做到这一点?

回答by Rick

It should be known that the only correct answers that have been given are the ones that included quotes around the attribute value, ie.

应该知道,给出的唯一正确答案是那些在属性值周围包含引号的答案,即。

$("[name='value']")

It is a mandatory requirement to include quotes around the value, see: http://api.jquery.com/attribute-equals-selector/

在值周围包含引号是强制性要求,请参阅:http: //api.jquery.com/attribute-equals-selector/

If you are using "name" in reference to the nodeName or "tag", then simply select by that string:

如果您使用“name”来引用 nodeName 或“tag”,则只需按该字符串进行选择:

$("div")
$("a")
$("p")
$("input")
$("body")

回答by Joseph Silber

Use this:

用这个:

$('[name=thename]');

Note:For performance reasons, it's better to specify the tag name. So, if you're looking for images with a name of winter, you'll use this:

注意:出于性能原因,最好指定标签名称。因此,如果您正在寻找名称为冬天的图像,您将使用以下命令:

$('img[name=winter]');

回答by Kris Krause

Not sure if you mean tag name or named attribute. So here is both -

不确定您的意思是标记名称还是命名属性。所以这里有两个 -

Attribute Equals Selector [name="value"]

属性等于选择器 [name="value"]

http://api.jquery.com/attribute-equals-selector/

http://api.jquery.com/attribute-equals-selector/

or Element Selector (“element”)

或元素选择器(“元素”)

http://api.jquery.com/element-selector/

http://api.jquery.com/element-selector/

// name attribute selector
$('[name=whatever]').each(function(index) {
  alert(index);
  // and of course depending on your element utilize $(this).doSomethingCool
});

// tag name selector for all h1 tags
$('h1').each(function(index) {
  alert(index);
  // and of course depending on your element utilize $(this).doSomethingCool
});

回答by emboss

If you mean the name attribute, it's this:

如果你的意思是 name 属性,它是这样的:

$('[name="value"]')

If you mean the element name, then it's like this:

如果你的意思是元素名称,那么它是这样的:

$('ul')

回答by jfriend00

In jQuery, you can use lots of things besides just id and class. The CSS3 selector specificationis full of other things you can use and jQuery even has some more things than this I believe. For example, you can use tag type, attribute values, position in the hierarchy. These are all valid jQuery selector objects:

在 jQuery 中,除了 id 和 class 之外,您还可以使用很多东西。该CSS3选择器规范是充满你可以使用和jQuery甚至有一些比这个,我相信更多的事情其他的事情。例如,您可以使用标记类型、属性值、层次结构中的位置。这些都是有效的 jQuery 选择器对象:

$("div")
$("div span")
$("[name=nyName]");
$("div span [name=nyName]");
$("ul li [name=nyName]");

In many cases, you will get better performance with selectors based on tag types, id values and class names because many browsers support searching for those in native code.

在许多情况下,使用基于标签类型、id 值和类名的选择器可以获得更好的性能,因为许多浏览器支持在本机代码中搜索这些。

回答by Daniel A. White

$('tagName')...

for <tagName />

为了 <tagName />

or

或者

$('[name="attributeName"]')...

for <input name="attributeName" />

为了 <input name="attributeName" />

The jQuery selector works like a CSS selector.

jQuery 选择器的工作方式类似于 CSS 选择器。

回答by Rafay

get the element by its name like

按名称获取元素,例如

$("div")

DEMO

演示

回答by Manmeet Khurana

To get the value, we can use multiple attributes, one of them being the name attribute. E.g

要获取值,我们可以使用多个属性,其中之一是 name 属性。例如

$("input[name='nameOfElement']").val();