最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get element without element id? - Stack Overflow

programmeradmin5浏览0评论
1.<input id="kw1"></input>

2.<input></input>

The first one, I can use document.getElementById to get the object, but on some websites, like the second, there is no id in element, but I also want to get the object. How can I do this ? And do not use JQuery

1.<input id="kw1"></input>

2.<input></input>

The first one, I can use document.getElementById to get the object, but on some websites, like the second, there is no id in element, but I also want to get the object. How can I do this ? And do not use JQuery

Share Improve this question edited Jul 4, 2014 at 8:10 Blisskarthik 1,2428 silver badges20 bronze badges asked Jul 4, 2014 at 8:03 CharlesXCharlesX 4484 gold badges7 silver badges15 bronze badges 4
  • document.getElementsByTagName('input') gives array of input elements. – Mr_Green Commented Jul 4, 2014 at 8:05
  • If you want to match that specific element, you will need something to distinguish it from the others. Either a matchable ancestor element (by id or class, for instance), or the index of that <input> element relative to the other elements of the same type. – Frédéric Hamidi Commented Jul 4, 2014 at 8:08
  • possible duplicate of How can I get query string values in JavaScript? – CharlesX Commented Sep 11, 2014 at 13:14
  • How strange that the DOM was designed with no inherent identification, no serial number, for each node. Was this to save a little space? Nodes have forward and backward links, so saving space was not a primary concern. Was it, then, an oversight? – David Spector Commented Aug 20, 2023 at 17:08
Add a ment  | 

4 Answers 4

Reset to default 9

you can do this by using :

getElementsByTagName

example :

var elements = document.getElementsByTagName('input'); // returns array

for more broader details in using it click here

Use document.getElementsByTagName('input') instead.

It'll return you an array with every input.

You can use document.getElementsByTagName('input'), but this give you all input elements.

But since you have nothing else to identify that element, you can not be more specific.

You can use:

document.getElementsByTagName("input");

which will return a list of objects of type "input"

发布评论

评论列表(0)

  1. 暂无评论