The most classic application is the general addEvent function:
<br>
function addEvent(element, type, handler) { if (element.attachEvent) { //IE8及以下浏览器 element.attachEvent('on' + type, handler); } else { //W3C标准浏览器 element.addEventListener(type, handler, false); } };
The function can determine whether to use attachEvent or addEventListener by detecting whether the attachEvent function exists. This It is also the simplest type of feature detection, so real-time detection is usually performed only when needed. Another kind of feature detection, because the detection process is more troublesome, will complete the detection in advance and save the detection result (usually Boolean type) in a variable.
The main goal of this article is to analyze and explain the new content of browser feature detection in jQuery1.4, and at the same time deepen the memory of several details in browser compatibility.
jQuery 1.4 mainly adds the following browser feature identifiers. This article analyzes them one by one:
Introduced in version 1.4, it determines whether a checkbox with no value set has a default value of "on".
Introduced in version 1.4.3, it determines whether the first option element of the select element will be selected by default Selected.
Introduced in version 1.4.3, it determines that when the select element is set to disabled, all its option children Whether the element will also be set to disabled.
Introduced in version 1.4.1, it determines whether the radio and checkbox will be used when using the cloneNode function on DocumentFragment The checked attribute is retained.
Introduced in version 1.4.3, it was decided that in IE the next block element has the hasLayout attribute and has display: inline;
, whether it will be displayed inline-block.
Introduced in version 1.4.3, it is decided that the next element in IE will have the hasLayout attribute and a fixed width /height, whether it will not be expanded by child elements.
Introduced in version 1.4.3, it is determined that when a td or th element is set to display: none;
, Is there any offsetHeight?
Use the following code to detect this feature:
<br>