JavaScript application instance for DOM (i) _dom

Source: Internet
Author: User
I'm sure that most people who come here to learn JavaScript are mostly trying to use this combination of the DOM elements in the page to do some really useful interactive effects. So I'm only here to teach you the most practical and useful JavaScript applications. But it's best to have some basic JavaScript or jquery programming. That's a lot of nonsense.
In today's first installment, we'll teach you how to use JavaScript to get DOM elements from a page. This is very important. I'll compare it to jquery.
If the element in the page is an id attribute
<div id= "Dom" ></div>

JQ Method: $ ("#dom"),
Native JS method: var a = document.getElementById ("Dom"); this a is equivalent to $ ("#dom");

If I want to get an element under the parent element
Copy Code code as follows:

<div id= "Dom" >
<span></span>
</div>

JQ Method: $ ("#dom span"),
Native JS method: var b = document.getElementById ("Dom"). getElementsByTagName ("span") [0]; this b is equivalent to $ ("#dom span")
There's a simple way to do it. var B = document.getElementById ("Dom"). Childnodes[0] But there's a problem in FF, and we'll talk about that later.

Get a set of elements in a page
Copy Code code as follows:

<div id = "Dom" >
<ul>
<li></li>
<li></li>
<li></li>
<ul>
</div>

JQ Method: $ ("#dom ul Li") or $ ("#dom Li") or $ ("#dom > Li"),
Native JS method: var c = document.getElementById ("Dom"). getElementsByTagName ("Li"); but this c is not the same as above, because it cannot be used directly as above JQ. You need to use a for loop to work together. If a single use, say I only use the first Li, only need var c = document.getElementById ("Dom"). getElementsByTagName ("li") [0], with the second is var C = document.getElementById ("Dom"). getElementsByTagName ("Li") [1], and so on. Because DOM elements are stored in the form of an array in JS.

The above are all well understood. Now I want to talk about this is commonly used by everyone. But in the original JS get is also the most troublesome attribute is the class attribute,
<div class = "dom" >
</div>

Jq method: Very simple $ (". Dom");
Native JS method: This is a bit of a problem, you need to write a function. Because native JS does not provide a way to get class directly. So we need that. I'll write the function first.
Copy Code code as follows:

function $class (domclass) {
var odiv = document.body.getElementByTagName ("*");
var A;
for (var i = 0;i<odiv.length;i++) {
if (Odiv.classname ==domclass) {
A = Odiv
}
return A;
}
}

Using this function to get it is very simple. only need var d = $class ("Dom");

Let me just say the meaning of this function,
var odiv = document.body.getElementByTagName ("*");
Get all the DOM elements in the page
Copy Code code as follows:

for (var i = 0;i<odiv.length;i++) {
if (Odiv.classname ==domclass) {
A = Odiv
}

This is to iterate through all the elements in the page and then compare them to their class. If it's the same as the domclass we're passing in, take this element to A;
return A;

So with the var d = $class ("Dom"), the equivalent of var d = A;

(By the way, ClassName is a property of this JS is the name of the DOM element Class)

All right, so much for today. I think my writing is more general. We must have a lot of people do not understand, there is not understand the direct question. I'll explain all of them. We want to learn what interactive effect can also tell me, I will try to meet everyone
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.