Home Web Front-end JS Tutorial Sharing of compatibility tips for DOM operations_DOM

Sharing of compatibility tips for DOM operations_DOM

May 16, 2016 pm 06:01 PM
DOM operation compatible

For example: when we get all li elements under ul. Or when it is the next element of an element. You may encounter this annoying space problem. Of course, these spaces will be automatically filtered in IE browser. FF, on the other hand, is not that hard-working. FF browser will treat these spaces as an element. If you are confused about space elements in the Dom, please run the following code. Use at least two browsers, IE and FF, to test. You will understand everything!

Copy code The code is as follows:




Dom


After running this code. You will find that 3. pops up in IE. Pop up in FF7.



  1. html

  2. css

  3. dom< ;/li>

<script> <br>var list = document.getElementById("list"); <br>var list_child = list.childNodes; //Get all in ol The child elements of <br>alert("There are "list_child.length" elements in ol, respectively"); <br>for(var i = 0; i<list_child.length; i ){ <BR>alert(list_child [i].tagName); <BR>} <BR></script>



The above code demonstrates the need Get all child elements in the ol element. And pop up several sub-elements in ol. We can see that ol contains 3 li elements. 3 pops up in IE. This is correct. So why does 7 pop up in FF and Chrome browsers? In fact, when you write code. A space will be formed between the element and the element wrap. (Note: Don’t think that a space will be formed by entering a carriage return. This is wrong. That is to say, the space between elements means that you just change the line hundreds of times. It is also counted as a space) FF and Chrome browsers will not Filter these space elements. So popping up 7 is also correct.
We use the same html structure below. To demonstrate how to filter and remove these space elements.
Copy the code The code is as follows:





Dom


After running this code. You will find that the results returned in IE, FF, and Chrome are the same.



  1. html

  2. css

  3. dom< ;/li>

<script> <br>function Del_space(elem){ //Function to filter spaces <br>var elem_child = elem.childNodes; //Get all child elements <br>for(var i = 0;i<elem_child.length;i ){ <BR>//If it is a text node and the content only contains spaces, delete the node <BR>if(elem_child[i].nodeName = = "#text" && ! /S/.test(elem_child[i].nodeValue)){ <BR>elem.removeChild(elem_child[i]);//If the element is a space, delete it <BR>} <BR>} <BR>} <BR>Del_space(document.getElementById("list")); //Delete all spaces in ol <BR>var list = document.getElementById("list"); <BR>var list_child = list.childNodes; //Get all child elements in ol <BR>for(var i=0;i<list_child.length;i ){ <BR>alert(list_child[i].tagName); <BR>} <BR></script>



The following method is recommended:
Copy code The code is as follows:





Dom


After running this code. You will find that the results returned in IE, FF, and Chrome are the same.



  1. html

  2. css

  3. dom< ;/li>

<script> <br>for(var x=0,list_li = document.getElementById('list').childNodes; x<list_li.length; x ) { <BR>if(list_li[x].nodeType == 1){ <BR>alert(list_li[x].tagName); <BR>} <BR>} <BR></script>
< ;/body>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The most stable version of Win10 The most stable version of Win10 Dec 25, 2023 pm 07:58 PM

Many users will encounter freezes or blue screens when operating the computer. At this time, we need to find the most stable win10 version to operate. Overall, it is very easy to use and can make your daily use smoother. The most stable win10 version in history 1. Win10 genuine original system. Here users can use simple operations. The system has been optimized and has strong stability, security and compatibility. Users can follow the steps to achieve the perfect machine. 2. Russian master streamlined The version of win10 has been strictly streamlined and many unnecessary functions and services have been deleted. After streamlining, the system has lower CPU and memory usage and runs faster. 3. Win10 Lite Edition 1909 is installed on multiple computers with different hardware models.

Is Switch2 compatible with Switch cartridges? Is Switch2 compatible with Switch cartridges? Jan 28, 2024 am 09:06 AM

Switch2 is a new model announced by Nintendo at Gamescom 2023. Some players are worried about whether there will be compatibility issues between the new model and the cartridges of previous versions. Let’s take a look. Is switch2 compatible with switch cassette? Answer: switch2 is not compatible with switch cassette. Introduction of Switch 2 cartridges According to information from Nintendo’s production chain company, Switch 2 may use 64GB cartridges. Because it has better performance and supports more 3A game masterpieces, it requires a larger cartridge capacity. Because many game works need to be castrated and compressed before they can be stuffed into a game cartridge. Moreover, Switch cartridges are prone to copying game content, so replace them with new cartridges.

Does Win11 support dynamic wallpapers? Does Win11 support dynamic wallpapers? Jan 01, 2024 pm 06:41 PM

As we all know, a major feature of win11 is its own Android subsystem, which allows us to install Android software without using an emulator. However, there is also the problem of win11 Android application lag. How should this be solved? Is win11 not compatible with dynamic wallpapers? Answer: Win11 is compatible with dynamic wallpapers. If it cannot be used, it may be because the software or system version is lagging behind. If it was just updated, it may be covered by the system wallpaper. 1. If the system or software version is lagging behind, just update the system and dynamic wallpaper software. 2. If it is covered by the system wallpaper, you can try to open "Settings" 3. Then enter the "Background" settings under "Personalization". 4. Then change the personalized background to "Picture" 5. After the modification is completed, you can set the dynamic wallpaper normally

Is Linux system compatible with Android software? Is Linux system compatible with Android software? Mar 20, 2024 pm 02:27 PM

In recent years, the popularity of the Android system in the field of mobile devices has grown rapidly, and many people have begun to pay attention to whether Android applications can also be run on other platforms. As a common operating system, Linux is favored by many people. So the question is, is the Linux system compatible with Android software? The first thing to make clear is that the Linux system and the Android system have certain similarities in the kernel. They are both operating systems based on the Linux kernel. Therefore, theoretically speaking, the Linux system can run

Methods to solve the incompatibility of d3dx9_43.dll Methods to solve the incompatibility of d3dx9_43.dll Feb 24, 2024 pm 10:06 PM

How to resolve d3dx9_43.dll incompatibility. In recent years, the rapid development of computers and games has allowed us to enjoy more entertainment and convenience. However, sometimes when installing or running certain programs, we may encounter some error messages, such as "d3dx9_43.dll is incompatible". In this case, how should we solve this problem? First, let’s understand what this error message means. d3dx9_43.dll is a system file of DirectX, which is used in the operating system

Introduction to the setting method of win10 compatible with win7 Introduction to the setting method of win10 compatible with win7 Jan 03, 2024 pm 05:09 PM

The reason why many friends are still choosing win7 system instead of win10 system is that they are afraid of poor compatibility. In fact, win10 system can now set the compatibility mode of win7. You only need to change the settings in the properties. Let’s take a look at it together. How to make win10 compatible with win71? First, right-click on the program we need to run under win7 system, then open "Properties" 2, then click "Compatibility" above to enter the compatibility tab. 3. Check "Run this program in compatibility mode" in compatibility mode. 4. Then select "Windows 7" from the drop-down menu below. 5. After finishing, just click "Apply" or click "OK".

How to deal with Eclipse version incompatibility How to deal with Eclipse version incompatibility Jan 04, 2024 am 10:29 AM

What should I do if I encounter an incompatible version of Eclipse? [Introduction] As a popular development tool, Eclipse not only provides powerful functions and rich plug-ins, but also has cross-platform features. However, when using Eclipse, you may sometimes encounter version incompatibility issues, which may cause the program to fail to run properly or compile errors. This article will describe how to deal with Eclipse version incompatibility and provide some code examples to solve common compatibility issues. [Solution] Update Ecl

Detailed guide to installing and configuring compatible network points in Win11 Detailed guide to installing and configuring compatible network points in Win11 Jan 09, 2024 pm 02:50 PM

There are many website users who need to use the old version of the browser when browsing, and the new version needs to add compatible outlets before they can be used, so today I will bring you a detailed tutorial on adding compatible outlets in win11. Come and learn. How to add compatible outlets in win11 1. First open the IE browser in the system, click the small gear in the upper right corner of the browser to enter "Settings". 2. Then in the settings menu that opens, open "Compatibility View Settings". 3. Enter the URL to be added in the edit box under Add this website, and then click "Add". 4. Finally, you can see the domain name of the added URL below, and you can access it directly by closing the window.

See all articles