Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Dom programming with Javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to realize Dom programming with Javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Javascript Dom programming

One: Document Object Model

In the DOM model, each element (element), attribute (attribute), and text (text) can be regarded as an object. Javascript can access these objects independently, and these objects can be found and changed in some ways.

DOM states that the label of each HTML is an element node, the text contained in the element is a text node, and each HTML attribute is an attribute node.

Two: access the node of DOM

A: get elements through ID

Vartarget=document.getElementById ("berenger")

B: get elements through TagName

VarlistItems=document.getElementsByTagName ("li")

ListItems is an array-like object, and you can use listItems.Length to get the length of the object.

C: get elements through ClassName

In most cases in Javascript Dom programming, it is easier to use className to get elements than tagname, but dom does not provide a corresponding function, so we will create a method.

Can be broken down into the following three steps:

1: search for all elements in the document.

2: for each element, compare the class you belong to with the class you are looking for.

3: if the same, add it to the list.

The js code is as follows:

VarCore= {}; Core.getElementsByClass=function (theClass) {varelementArray= []; / IE does not support getElementsByTagName ("*"), use document.All. If (document.all) {elementArray=document.all;} else {elementArray=document.getElementsByTagName ("*");} varmatchedArray= []; varpattern=newRegExp ("(^ |)" + theClass+ "(| $)"); for (vari=0;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report