Browser share status quo
In the development process of the major browser manufacturers, they actually have different implementation of the Web standard, because the implementation of the standard is different, so there will be compatibility, early IE is in the browser world, occupy the dominant position. So it implements a lot of things in itself that are different from standard browsers, CSS and JS.
Js compatibility problems and solutions
1, js style, currentStyle and getComputedStyle differences
style
: all browsers are compatible, you can set styles and get styles, but you can’t get external styles.
currentStyle
: This property is only compatible with Internet Explorer, not Fox and Google
ele.currentStyle.attr
;
getComputedStyle
: This property is compatible with Firefox Google, not IE8 and below
window.getComputedStyle(ele,null).attr
getComputedStyle
Read the style, passelement.style
Modify the style.
::after, ::before
getComputedStyle(oDiv, '::after').content;
//
function getStyle(ele,attr){
if(ele.currentStyle){
return ele.currentStyle[attr]
}else{
return getComputedStyle(obj,false)[attr]
}
}
2. Use event objects
var e = arguments.callee.caller.arguments[0] || window.event
arguments.callee.caller.arguments[0]:
argument.callee.caller.arguments[0]
This is the first parameter event in the pass set.
3. Get target element:
event.srcElement ? event.srcElement : event.target;
4. Of attachEvent and addEventListener
ele.attachEvent('onclick',function(){
console.log('test... ')
})// Ie11 and above are not supported
ele.addEventListener("click",function(){
console.log('ceshi... ')
},false) // Internet Explorer 8 and below are not supported
5. Obtain DOM node:
parentElement
Gets the parent element in the object hierarchy.
parentNode
Gets the parent in the document hierarchy.
parentElement
Looking for theThe element, so when the root document is found, an error is reported with a value of null, and
parentNode
Looking for thenode, of course, you can display it!
bodyDom.parentNode.parentNode.parentNode.parentNode
bodyDom.parentElement.parentElement.parentElement.parentElement
—null
6. Date function processing
new Date().getFullYear() === new Date().getYear()
new Date().getYear()
For the 119
7, collection class object problem
8, compatible mouse button coding
function but(evt){
var e = evt || window.event;
if(evt){
return e.button;
} else if (window.event){
switch(e.button){
case 1: return 0;
case 4: return 1;
case 2: return 2;
}
}
}
Other questions raised by 9, 8:
window.event
The difference between a global object and an event access object (the evT argument passed) :
srcElement
Properties.
CSS Compatibility Problems
is located on the first line of the HTML document, before the < HTML > tag. Tell the browser’s parser what document to use to parse the document. A non-existent or improperly formatted DOCTYPE will cause the document to be rendered in compatibility mode.
1, the reset
The most important and the most common, is that the browser default support for tags is different, so we want to unify, will be CSS reset. But don’t reset just for reset’s sake:
1.1 The default attributes of elements are set to one side, such as div padding and margin 0, which is not necessary
The CSS reset system also writes the Settings for some of the most unusual elements.
2. Overlap between upper and lower margins
Two adjacent divs margin-left margin-right will not overlap, but adjacent margin-top margin-bottom will.
3, TD height problem
Td width in table does not contain the width of border, but TD height in oprea and FF contains the height of border.
4, compatible with IE8 or IE9 or any VERSION of IE browser
<! --[if gte IE 8]><! [endif]-->
)
<! DOCTYPE html>
<! --[if IE 8 ]> <html class="ie8" lang="en"> <! [endif]-->
<! --[if IE 9 ]> <html class="ie9" lang="en"> <! [endif]-->
<! --[if (gt IE 9)|!(IE)]><! -->
<html lang="en"> <! - <! [endif]-->
<! --[if !IE]><-->
<div class="box" id="box"></div>
<! [endif]-->
5. For the 360 dual-core browser
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="renderer" content="webkit">
6, IE8 CSS compatibility
6.1 Using meta Tags to Adjust the Browser Rendering Mode:
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="renderer" content="webkit">
6.2 Some CSS in Internet Explorer 8 are not supported
first-child
, but not supported:last-child
. Because the former is the CSS2.1 standard, the latter is the CSS3 standard.The recommended approach is not to use last-child, but to give the last element a.last class and then style it so that it’s all compatible.
html5shiv.js
<header>
,<nav>
Such tags cannot be rendered in IE8. Html5shiv.js helps ie6-8 browsers to be compatible with HTML5 semantic tags.
Respond.js
min/max-width
“Media search criteria.
Respond.js
. andRespond.js
The earlier the page is referenced, the less likely the user will see the page flicker.
rem.js
7, placeholder
Versions below IE10 – are not supported (the prompt can be simulated by using a SPAN tag.)
About CSS optimization:
1. Don’t use wildcards like *{}
2. Minimize the use of high performance attributes such as absolute positioning and floating
Use CSS inheritance to reduce the amount of code
CSS Sprites reduce HTTP requests
5, co-write CSS:
6. Don’t restrict ID rules with tags or classes
7. Avoid wildcard selectors
#header a {font-weight:blod; }
#header a {font-weight:blod; }
* { font-size:20px; }
.selected * {color: red; }