Cross-browser Development Experience Summary (3) be alert to "IE dependency syndrome" _ javascript skills

Source: Internet
Author: User
Cross-browser Development Experience Summary (3) be alert to "IE dependency syndrome" DHTML
DHTML is a good thing. It greatly enhances the front-end interaction implementation, making it extremely simple to get page elements and modify page elements dynamically. But do all browsers know these syntaxes?
Document. all
Document. all is not recognized by all browsers. To write more common code, you 'd better use the id to get it. Use document. getElementById (...)
Element. outerText, element. innerText, element. outerHTML, element. innerHTML
Element. outerText, element. innerText, and element. outerHTML are unique to IE, while element. innerHTML is common.
If you want to use non-generic properties in other browsers, refer to the following code:

The Code is as follows:


If (! IsIE ()){
HTMLElement. prototype. _ defineGetter _ ("innerText ",
Function (){
Var anyString = "";
Var childS = this. childNodes;
For (var I = 0; I If (childS [I]. nodeType = 1)
AnyString + = childS [I]. innerText;
Else if (childS [I]. nodeType = 3)
AnyString + = ahildS [I]. nodeValue;
}
Return anyString;
}
);
HTMLElement. prototype. _ defineSetter _ ("innerText ",
Function (sText ){
This. textContent = sText;
}
);
}


Document. forms. actionForm. inputName. value
Document. all. title. value to obtain the value of the title input field in the form named actionForm. It should be changed to document. forms. actionForm. input. to use this method, make sure that the form tag in HTML has a complete closed relationship with other tag structures.
Table operations
MoveRow (iSource, iTarget) Method
ORow = tableObject. moveRow (iSource, iTarget). This method can easily adjust the dynamic sequence of tr in table. But this method is implemented by the IE kernel itself, not the DOM standard method, so no other browsers. To use these methods that are unique to IE, either use the standard DOM node control method-insertBefore (currobj, beforeObj. nextSibling), or first implement a moveRow method on the prototype of the HTMLDocument class:

The Code is as follows:


Function getTRArray (){
......
// Put the tr to be manipulated into an array as the return value of this method
}

Function getTRByIndex (sourceELIndex ){
Var trArray = getTRArray ();
Var result = trArray [sourceELIndex];
Return result;
}

If (! IsIE & HTMLElement. moveRow = null)
{
// Input parameter description:
// SourceELIndex: Specifies the row (> = 1) of the tr to be moved in the tbody)
// TargetELIndex: number of rows to be moved to the tbody (> = 1, <= number of rows)
HTMLElement. prototype. moveRow = function (sourceELIndex, targetELIndex)
{
Var tbObject = document. getElementById ("tbodyEL ");
Var resultEL;

If (sourceELIndex> = targetELIndex)
{// Move up
Var s = sourceELIndex-1;
Var t = targetELIndex-1;
} Else {
Var s = sourceELIndex-1;
Var t = targetELIndex;
}
Var sourceEL = getTRByIndex (s );
Var targetEL = getTRByIndex (t );
// Alert ("begin" + sourceELIndex + targetELIndex );
// Alert ("begin" + s + t );
TbObject. insertBefore (sourceEL, targetEL );
ResultEL = sourceEL;
Return resultEL;
}
}

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.