我想从web page.i中获取所有元素,使用表单id或name来获取所有元素,但如果没有表单id或名称,则无法从该页面获取元素.这个问题的替代方案是什么.请帮忙.
您可以使用检索html文档中所有节点的数组document.getElementsByTagName('*').之后,您可以遍历该数组:
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)
}
Run Code Online (Sandbox Code Playgroud)
2014年更新:更现代化的方法
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)
});
Run Code Online (Sandbox Code Playgroud)