Different _javascript techniques for JS and CSS in IE and Firefox (FF)

Source: Internet
Author: User
JS and CSS are different in IE and Firefox (FF)
CSS:
1.
The UL tag in FF has a padding value, but there is no margin value, and in IE is the opposite
Solution: The UL padding and margin are set to 0 (also can not 0) such as: Padding:0;margin:0;list-style:none;
JS:
1.
IE innertext in FF, using Textcontent;
eg
Copy Code code as follows:

var Obj=document.getelementbyid ("_td");
var text;
if (Obj.innertext) {
Text=obj.innertext;
else if (obj.textcontent) {
Text=obj.textcontent;
}

2.
Return object state in Ajax IE can use readystate but must be readystate in FF, so it is best to write readystate
3. Get the keyboard return value in IE and FF,
<input type= "text" onkeyup= "Test (event)"/>
function Test (e) {
var keyc=getkeycode (e);
alert (KEYC);
}
function getKeyCode (e) {//Get keyboard event values from different browsers
var keyc;
if (window.event) {//ie keyboard event
Keyc=e.keycode;
else if (E.which) {//Firefox
Keyc=e.which;
}
return KEYC;
}
4. Add a removal event for an object
var Obj=document.getelementbyid ("_tname ');
To add an event:
if (obj.attachevent) {
Obj.attachevent ("onchange", function () {
Otherfunction (params);//Here you can give the method arguments, or you can call other methods directly.
});
else if (Obj.addeventlistener) {
Obj.addeventlistener ("Change", function () {
Otherfunction (params);
},false);
}
To remove an event:
Obj.onclick=null;
* The following code why not, IE output obj.onclick unexpectedly for Anonymous, hope the master can help solve
if (obj.detachevent) {
Obj.detachevent ("onchange", test);
else if (Obj.removeeventlistener) {
Obj.removeeventlistener ("Change", Test,false);
}*/
5.
Event.x and Event.y in IE
Only event.pagex,event.pagey in ff.
All have Event.clientx and Event.clienty properties.
Solution:
var x=e.x?e.x:e.pagex;//e parameters that are passed in for the event object
The Input.type property under 6.IE is read-only, but MF can modify
7. In IE, Getelementsbyname (), (Document.all[name) (not successfully tested) can not be used to obtain DIV elements (whether there are other elements that cannot be taken is unknown).
8. Through JS to trigger events
<script type= "Text/javascript" ><!--
function Handertoclick () {
var Obj=document.getelementbyid ("Btn1");
if (document.all) {//ie
Obj.fireevent ("onclick");
} else {
var e=document.createevent (' MouseEvent ');
E.initevent (' click ', False,false);
Obj.dispatchevent (e);
}
}
--></script>
<input type= "button" value= "Btn1 id=" BTN1 "onclick=" alert (' button btn1 Click event ') "/>
<input type= "button" value= "triggers the onclick event with BTN1 id" onclick= "handertoclick ()"/>
9.IE Event object has srcelement attribute, Firefox, event object has target attribute
var obj=e.srcelement?e.srcelement:e.target;//e parameters that are passed in for the event object
It's not tested from the bottom here.
10. Attributes defined in FF must be getattribute ()
11. Node problem
Parentelement Parement.children is used in IE, and parentnode is used in FF Parentnode.childnodes
ChildNodes the meaning of the subscript is different in IE and FF, FF uses the DOM specification, and a blank text node is inserted in ChildNodes.
There is no Removenode method for nodes in FF, you must use the following method Node.parentNode.removeChild (node)
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.