Using getElementsByName without starting at document root
Is 开发者_运维知识库there a way to use getElementsByName
without starting from the DOM root.
For example I have a div element, and want to start searching from that element.
If not, then do I have to write my own function that recursively iterates through the child nodes, or is there a different way to do it.
getElementsByName
and getElementById
are both members of the document
object, and aren't part of the HTMLElement
prototype. In modern browsers (IE8, Firefox, Chrome, Opera), you can use element.querySelectorAll("*[name='myName']")
.
Other than that your alternative is to use a library like Sizzle or a framework such as jQuery (which uses Sizzle) to handle selectors.
This doesn't really make any sense, because the value of each "id" attribute in every tag in the document must be unique.
Because of that, it doesn't matter where you "start".
edit — never mind, if you're talking about getElementsByTagName
Basically its is DOM, so every time you search for some element by NAME or ID, it can be started from some reference point.
Say you want some element from Form named as Form1
, then you can write
document.Form1.fieldName
which will give you particular field.
But it all should be absolute path. Because if you do have some reference, you can refer to its children.
Jquery anf yUI provides better support for fetching and setting of DOM elements. JQuery eases the things which are hard in javascript.
try www.jquery.com
thanks.
have you tried.
document.getElementsbyTagName("div")[0].getElementsbyName("your search");
also with jQuery selectors the search for dom elements become quite easier.
精彩评论