How do I get the border-radius from an element using jQuery?

后端 未结 3 869
梦谈多话
梦谈多话 2021-01-04 13:59

I\'ve got a div, with the following HTML and CSS. In an attempt to make my Javascript code more robust, I\'m attempting to retrieve certain CSS attributes from the selected

3条回答
  •  鱼传尺愫
    2021-01-04 14:44

    I'm guessing it's not officially supported yet as it's a bit unpredictable... In Firefox using $("#somediv").css("-moz-border-radius", "20px"); will set the border radius fine, but trying to read it back via $("#somediv").css("-moz-border-radius"); returns an empty string... However, it appears that Firefox explodes the border radius into its component parts meaning $("#somediv").css("-moz-border-radius-topleft"); will work (obviously only returns one corner's settings though).


    Edit:

    As Tgr points out $('#foo').css('MozBorderRadius') will let you read it back generically in Firefox. And as Bradley Mountford points out in a comment below, it looks like you can read from Webkit using the component parts too (although only chrome seems to like border-top-left-radius, where as both Chrome & Safari handle -webkit-border-top-left-radius...

    So summarising, you get the following (based on your 5px setting):

    In Firefox:

    $("#somediv").css("MozBorderRadius");             //"5px 5px 5px 5px"
    $("pre").css("-moz-border-radius-topleft");       //"5px"
    

    In Webkit (tested in Chrome & Safari):

    $("pre").css("-webkit-border-top-left-radius");   //"5px 5px"
    

提交回复
热议问题