My main question is to calculate the last warning message, but any other information is also welcome.
I am trying to learn to use javascript (to use later with greasemonkey), but I am struggling to understand the dom and how to process it slightly.
& lt; Html & gt; & Lt; Top & gt; & Lt; Script type = "text / javascript" & gt; Function my_test () {var element = document.getElementsByTagName ("div"); // print "[object HTMLCollection] with length 8" warning ("found" + element + "length + + elements length); // print" 0: [object HTMLDivElement] "warning (" 0: " + Element [0]); // How to make the following calculation? Alert ("AAAA and three CCCC for Intertext");} & lt; / script> gt; & lt; / head & gt; & LT; Body & gt; div class = "interesing"> aaa gt; div class = "two" & gt; BBBB CCCC & lt; / div & gt; & lt; div class = "boring" & lt; / div & gt; 1111 3333 & gt; Input type = "button" onclick = "my_test ()" value = "my test" & lt; / body > element is now an array of elements and I can access each of them individually But where can I get the properties / properties of these elements?
Assume that you want to capture the elements and not your children and status by class, I'm just a I will write a small function which will return the collection of elements of the class.
& lt; Html & gt; & Lt; Top & gt; & Lt; Script type = "text / javascript" & gt; Function my_test () {var element = document.getElementsByTagName ("div"); // print "[object HTMLCollection] with length 8" warning ("found" + element + "length + + elements length); // print" 0: [object HTMLDivElement] "warning (" 0: " + Element [0]); // How to compute the following: "++" ("one", c ("intersection") [0]) [0] .winnerHTML + "and" + "three" + " "C" ("three", C ("intrace") [0]. [0] .winnerHTML);} function C (CLS, ELM) {if (ELM) ELM = document; if (elm.getElementsByClassName) Elm.getElementsByClassName (CLS) returns; Other {var t = []; Var kids = elm.getElementsByTagName ("*"); (Var i = 0; i gt; & gt; & gt; & gt; Body & gt; & lt; div class = "interesing" & gt; & lt; div class = "A" & gt; AAAAA & lt; div class = "two" & gt; BBBB & lt; / div & gt; & lt; div class = "three" & gt; CCCC & Lt; / div & gt; & lt; / div & gt; & lt; div class = "boring" & gt; & Lt; Div class = "one" & gt; 1111 & lt; / Div & gt; & Lt; Div class = "two" & gt; 2222 & lt; / Div & gt; & Lt; Div class = "three" & gt; 3333 & lt; / Div & gt; & Lt; / Div & gt; & Lt; Input type = "button" onclick = "my_test ()" value = "my test" & lt; / Body & gt; & Lt; / Html & gt;
Please note that the C
function I wrote to get the element by the class is not the right solution, but it will work well in most cases and it It is easy to write on the fly first by getting the first element with the code interesing
( c ("interesing" [0]
) and then to get the element of the first child Works as a parameter by using it in a
( c ("one", c ("intre Ning is a section of [0]) [0]
).
If you want to capture those elements based on the position or tag, then a different solution would be more suitable. I'm assuming that you are looking for elements that, based on class, edit a simple getElementsByClassName
function
: It looks like I have a solution to your warning question But have not answered a lot about the real question ... I think that you getElemen TsByTagName
can be stringed together, so if you want div
to kids, you can simply run the loop for the nest, all div
, and If there are children, cycles through them.
Here's an example:
var element = document.getElementsByTagName ("div"); (Var i = 0; i & lt; elements.length; i ++) {var str = "parent div (" + element [i] .className + ")"; Var children = element [i] .getElementsByTagName ("div"); If (children.length) {str + = "children:"; (Var j = 0; j & lt; kids.length; j ++) {str + = "{className:" + children [j] .className + ", internal HTML:" + children [J]. WinnerHTML + "},"; }} And {str + = "no child."; } Alert (str); }
Comments
Post a Comment