AddRule's compatibility in firefox _ javascript skills

Source: Internet
Author: User
The compatibility of addRule in firefox is now a lot of ways to control html element styles using scripts.
For a single element, you can directly modify an element. style. display =... or element. className =... to modify multiple styles.
You can use a script to directly import the css file to modify the style of multiple elements.
The current project has such a requirement. It is required to change the style of all child nodes under a certain p, but not the style of p itself.
I did not want to leave the code below:

The Code is as follows:





Pp

Spanspan
Ttttt



Transfer


This code has been working very well until one day my colleague told me that my module crashed his browser.

I took a flashlight and looked for it again in the code of more than two thousand lines with a magnifier ..
I couldn't find the problem, and I had no doubt about the above Code.
The p id is directly read from the database, which is a number, for example

And so on.
I used to test the characters in ie6 and ff1.5 for the id,

The Code is as follows:






All such id settings can be used
Document. getElementById ("A shun ")
Document. getElementById ("??? "),
Document. getElementById ("-1 "),

There is no problem with reading, not to mention pure numbers.


Finally, after repeated debugging, find the culprit addRule.
Document. styleSheets [0]. addRule ("# a1 *", "background-color: red ")
Document. styleSheets [0]. addRule ("#123 *", "background-color: red ")
No problems
The following line of code will let ie die. The memory and virtual memory have been skyrocketing. I have 512 of the memory, and all of a sudden xp prompts that the virtual memory is not enough.
Document. styleSheets [0]. addRule ("#-1 *", "background-color: red ");
Similarly, in ff1.5, the function will fail, but will not crash.
Summary:
1. Ie and ff are quite tolerant of id values. IDS can be almost any character, Chinese, English, letters, numbers, negative numbers, and special characters.
2. However, in the addRule function, if the id is negative, both ie and ff will fail and ie will crash.
Appendix:
Compatibility of addRule in ff

The Code is as follows:







Pp

Spanspan
Ttttt



Script
Function f (){
Var rule = "# a1 * {background-color: red }";
Var index.html document.stylesheets%0%.css Rules. length;
Document. styleSheets [0]. insertRule (rule, index );
}
Script

Transfer
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.