jQuery和用速记设置CSS [英] jQuery and Setting CSS with Shorthand

查看:68
本文介绍了jQuery和用速记设置CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自:Pro PHP和jQuery:


■提示返回的值是CSS简写属性.3 jQuery增加的
奖金是使用CSS
简写设置CSS属性的能力,使用基本JavaScript无效。

■ Tip The values returned are CSS shorthand properties.3 An added bonus of jQuery is the ability to set CSS properties using CSS shorthand, which doesn't work using basic JavaScript.

来自jQuery API参考:


简写CSS属性(例如边距,背景,边框)不支持
。例如,如果要检索渲染的边距,
使用:$(elem).css('marginTop')和$(elem).css('marginRight'),以及
on。

Shorthand CSS properties (e.g. margin, background, border) are not supported. For example, if you want to retrieve the rendered margin, use: $(elem).css('marginTop') and $(elem).css('marginRight'), and so on.

我很困惑,可以设置css简写,但不能读取?当然我只会使用样式表,但问题是学术上的启发。

I'm confused, css shorthand can be set, but not read? Of course I'll simply use a stylesheet, but, the question is for academic edification.

推荐答案

如果你打开控制台在firefox或chrome上。输入:

Open up the console if you are on firefox or chrome. Type this:

jQuery(".post-text"); // will select the post of this answer, you should see it in the console
jQuery(".post-text").css("margin"); // will be "", the shorthand returns NOTHING
jQuery(".post-text").css("margin-top"); // will be "0px", the real notation works
jQuery(".post-text").css("margin","10px 10px 10px 10px"); // we just set all margins with shorthand, you should notice a change in the UI of stackoverflow.
jQuery(".post-text").css("margin-top"); // is "10px" now
jQuery(".post-text").css("margin"); // is still ""

我希望这可以澄清这个概念。简短的回答:你可以设置,但不能获得简写的css礼仪。

I hope this clarifies the concept. Short answer: You can set, but not get shorthand css proprieties.

这篇关于jQuery和用速记设置CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆