The 1.6.3 of the CSS method in JQuery (_jquery) to the implementation flaw of the float

Source: Internet
Author: User
jquery's CSS method unifies two kinds of writing, uses the float attribute directly, the following CSS method passes the parameter "float" to be able to set up also can obtain the element float.
Copy Code code as follows:

<div id= "D1" >float div</div>
<script type= "Text/javascript" >
$ (' #d1 '). css (' float ', ' right ');
var str = $ (' #d1 '). css (' float ');
alert (str);
</script>

But jquery has to be smart, plus support for cssfloat and stylefloat, see API documentation, like getting the float property of an element, the following is equivalent.
1 $ (' Div.left '). css (' float ');
2 $ (' Div.left '). CSS (' cssfloat ');
3 $ (' Div.left '). CSS (' stylefloat ');
But mode 3 returns different values in each browser, such as using stylefloat to get floating
Copy Code code as follows:

<div id= "D1" >float div</div>
<script type= "Text/javascript" >
Alert ($ (' #d1 '). CSS (' stylefloat ');
</script>

The element div does not have a float set, so the default should return ' None '. But the result is
IE6/7/8: Returns the empty string "None"
Ie9/firefox/safari/chrome/opera: Returns an empty string
Also, use Cssfloat to set the float:
Copy Code code as follows:

<div id= "D1" >float div</div>
<script type= "Text/javascript" >
$ (' #d1 '). CSS (' cssfloat ', ' right ');
</script>

IE6/7/8: Setup was unsuccessful
Ie9/10/firefox/safari/chrome/opera: Set Success
Also, use stylefloat to set the float:
Copy Code code as follows:

<div id= "D1" >float div</div>
<script type= "Text/javascript" >
$ (' #d1 '). CSS (' stylefloat ', ' right ');
</script>

Ie6/7/8/9/10/opera: Set Success (Opera is a freak, stylefloat and cssfloat support)
Firefox/safari/chrome: Setup is unsuccessful
Therefore, use a CSS method to get or set floats to avoid differences between browsers or to use float. Do not use stylefloat or cssfloat.
Of course, if this is a jquery bug, then fixing is easy too.
1, modify the Jquery.css method, add a stylefloat judgment.
Copy Code code as follows:

Cssfloat needs a special treatment
if (name = = = "Cssfloat" | | | name = = = "Stylefloat") {
name = "float";
}

This uses $ (xx). CSS ("stylefloat") there is no compatibility problem.
2, modify the Jquery.style method, add a judgment if Stylefloat or cssfloat are converted to float
Copy Code code as follows:

Don ' t set styles on text and comment nodes
if (!elem | | elem.nodetype = = 3 | | elem.nodetype = 8 | |!elem.style) {
Return
}
This is the plus fix code.
if (name = = = "Cssfloat" | | | name = = = "Stylefloat") {
name = "Float"
}
Make sure so we ' re working with the right name
VAR ret, type, OrigName = jquery.camelcase (name),
style = Elem.style, hooks = jquery.csshooks[OrigName];

This uses $ (xx). CSS ("Cssfloat", "right") or $ (xx). CSS ("stylefloat", "right") each browser is OK.
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.