How to set browser specific css in JavaScript?

前端 未结 5 1216
再見小時候
再見小時候 2021-01-13 17:48

How to set style -webkit-box-shadow: 0px 0px 11px #000 to an element via JavaScript?

相关标签:
5条回答
  • 2021-01-13 18:19

    Since I haven't yet seen a code example that works without jQuery, here's one:

    document.getElementById("test").style.webkitBoxShadow = "0px 0px 11px #000";
    

    Note that the javascript property is not the same as the CSS property. The javascript properties do not use a dash and use camelCase instead.

    And, a working demo here: http://jsfiddle.net/jfriend00/4LT7u/

    0 讨论(0)
  • 2021-01-13 18:20

    You can set it using the style object:

    element.style['-webkit-box-shadow'] = '0px 0px 11px #000';
    

    Demo: jsfiddle.net/S2eLb/

    Edit:

    To be clear, this method does not require the jQuery library.

    0 讨论(0)
  • 2021-01-13 18:21

    Use CamelCase like this:

    WebkitBoxShadow
    

    Generally, a -<lowerCaseLetter> translates to the corresponding uppercase letter in the JS API.

    0 讨论(0)
  • 2021-01-13 18:23
    $('#elementname').css({
            '-webkit-box-shadow':'0px 0px 11px #000'})
    
    0 讨论(0)
  • 2021-01-13 18:30

    You can use the .css

    $("divClass").css("-webkit-box-shadow","0px 0px 11px #000");
    

    Links: http://api.jquery.com/css/

    0 讨论(0)
提交回复
热议问题