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

javascript - How to get all elements between html tag? - Stack Overflow

programmeradmin4浏览0评论

I want to get all elements from the web page.i used form id or name to get all elements but if there is no form id or name i can't get elements from that page.what is the alternate for this problem. please help.

I want to get all elements from the web page.i used form id or name to get all elements but if there is no form id or name i can't get elements from that page.what is the alternate for this problem. please help.

Share Improve this question edited Nov 30, 2011 at 6:41 Kiran 20.3k11 gold badges71 silver badges101 bronze badges asked Nov 30, 2011 at 6:36 Manohar Kulanthai velManohar Kulanthai vel 5814 gold badges9 silver badges20 bronze badges 5
  • What are you exactly looking for? If you have the document surely you have all the elements? – Emond Commented Nov 30, 2011 at 6:41
  • 1 $("*") :-D __________ – Adam Rackis Commented Nov 30, 2011 at 6:42
  • @AdamRackis But that assumes jQuery use. Manohar might not be using that. Although, it is the best way to do that :) – Arindam Commented Nov 30, 2011 at 6:44
  • I mean all the objects type and name or value – Manohar Kulanthai vel Commented Nov 30, 2011 at 6:44
  • @Arindam - I know, hence my smiley face...though from the answers I'm seeing, maybe I should have just put that as an answer – Adam Rackis Commented Nov 30, 2011 at 6:46
Add a ment  | 

5 Answers 5

Reset to default 6

You can retrieve an array of all nodes in a html document using document.getElementsByTagName('*'). After that you can iterate through that array:

var allElements = document.getElementsByTagName('*');
for (var i=0;i<allElements.length;i++){
  //do things with the element, e.g.
  //console.log(allElements[i].type)
  //console.log(allElements[i].id)
  //console.log(allElements[i].innerHTML)
}

Update 2014: a more modern approach would be

var allEls = [].slice.call(document.querySelectorAll('*');
allEls.forEach( function (el) {
      //do things with the element, e.g.
      //console.log(el.type)
      //console.log(el.id)
      //console.log(el.innerHTML)
});

You can use jQuery: $("html *") which will return all elements between the html tags

for names you must use $("html *").attr('name')
for values $("html *").val() or $("html *").attr('value')

Have heard of something called jQuery ? With the help of traversing API you can get to an element you want. Here is the plete list of API - http://api.jquery./

You can use the jQuery selectors like: $("*") .This particular selector will return all the elements(tags) of that html page.

maybe you can try this: document.childNodes

no framework, lib needed.

发布评论

评论列表(0)

  1. 暂无评论