今天碰到一个 使用 jq 动态的给元素添加样式,一开始记得之前这个遇到过,好久不用又忘记了,还是选择记下来,加深自己的印象。
一般写法是这样的:
1 $('#footer’).css({ 2 position: 'fixed', 3 width: '100%', 4 bottom: 0, 5 left: 0, 6 overflow: 'hidden', 7 zIndex:'99 !important' 8 })
上面是我一开始常规写法,发现没有作用,正确的写法应该是:
1 $("#footer").css("cssText","position:fixed;width:100%;bottom:0;left:0;overflow:hidden;z-index:99!important");
使用 cssText 这个属性。
可以参考这个博客了解 cssText 属性