开发者

how to get elements by name using jquery?

开发者 https://www.devze.com 2023-04-01 20:34 出处:网络
how to get html element by name using开发者_JAVA技巧 jquery without using id or class ? Is there any way to do that?It should be known that the only correct answers that have been given are the ones t

how to get html element by name using开发者_JAVA技巧 jquery without using id or class ? Is there any way to do that?


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/

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

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


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]');


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

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

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

$('ul')


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

Attribute Equals Selector [name="value"]

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

or Element Selector (“element”)

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
});


$('tagName')...

for <tagName />

or

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

for <input name="attributeName" />

The jQuery selector works like a CSS selector.


In jQuery, you can use lots of things besides just id and class. The CSS3 selector specification is 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:

$("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.


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

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


May be this:$('div[name="element_name"]').

Or other element type like:

img: $('img[name="element_name"]')

td: $('td[name="element_name"]') ...


get the element by its name like

$("div")

DEMO

0

精彩评论

暂无评论...
验证码 换一张
取 消