Visibility attribute question

前端 未结 3 1887
挽巷
挽巷 2021-02-03 23:17

What is the difference between

$(\"#myid\").attr(\"style\", \"visibility: hidden\")

and

$(\"#myid\").css(\"visibility\", \"hidden\"

3条回答
  •  名媛妹妹
    2021-02-04 00:17

    Doing this:

    $("#myid").attr("style", "visibility: hidden")
    

    Will leave only this style attribute, while doing this:

    $("#myid").css("visibility", "hidden")
    

    Will add (or set) this style attribute.

    Here's an example, the first will always result in this:

    style="visibility: hidden;"
    

    The second just adds visibility so your style may now be:

    style="width: 50px; color: red; visibility: hidden;"
    

提交回复
热议问题