Answers:
恐怕没有内置的方法。您可以执行以下操作:
var width = ( 100 * parseFloat($('.largeField').css('width')) / parseFloat($('.largeField').parent().css('width')) ) + '%';
class="largeField"
到跨度,当前您正在选择一个空集。
最简单的方法
$('.largeField')[0].style.width
// >>> "65%"
style="width:65%"
)。
这绝对是可能的!
您必须首先hide()父元素。这将阻止JavaScript计算子元素的像素。
$('.parent').hide();
var width = $('.child').width();
$('.parent').show();
alert(width);
看我的例子。
现在...我想知道我是否是第一个发现这种黑客:)
更新:
element.clone().appendTo('body').wrap('<div style="display: none"></div>').css('width');
它将在</body>
标签之前留在隐藏元素之后,您可能希望这样做.remove()
。
请参阅单线示例。
我愿意接受更好的主意!
您可以访问该document.styleSheets
对象:
<style type="text/css">
.largeField {
width: 65%;
}
</style>
<script type="text/javascript">
var rules = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
for (var i=0; i < rules.length; i++) {
var rule = rules[i];
if (rule.selectorText.toLowerCase() == ".largefield") {
alert(rule.style.getPropertyValue("width"));
}
}
</script>
styleSheets[0]
)。
for (var i=0; rules.length; i++)
办?不应该for (var i=0; i<rules.length; i++)
一个基于Adams的jQuery插件:
(function ($) {
$.fn.getWidthInPercent = function () {
var width = parseFloat($(this).css('width'))/parseFloat($(this).parent().css('width'));
return Math.round(100*width)+'%';
};
})(jQuery);
$('body').html($('.largeField').getWidthInPercent());
将返回“ 65%”。如果您喜欢if(width =='65%'),则仅返回四舍五入的数字才能更好地工作。如果您直接使用Adams的答案,那将是行不通的(我得到类似64.93288590604027的信息)。:)
基于timofey出色且令人惊讶的解决方案,这是一个纯Javascript实现:
function cssDimensions(element)
var cn = element.cloneNode();
var div = document.createElement('div');
div.appendChild(cn);
div.style.display = 'none';
document.body.appendChild(div);
var cs = window.getComputedStyle
? getComputedStyle(cn, null)
: cn.currentStyle;
var ret = { width: cs.width, height: cs.height };
document.body.removeChild(div);
return ret;
}
希望它对某人有帮助。
{
在第一行末尾忘记了。
您可以使用css(width)函数返回元素的当前宽度。
即。
var myWidth = $("#myElement").css("width");
另请参阅:http : //api.jquery.com/width/ http://api.jquery.com/css/
jQuery中没有任何内容,即使在javascript中也没有任何直接的内容。以timofey的答案并运行它,我创建了此函数,该函数可用于获取您想要的任何属性:
// gets the style property as rendered via any means (style sheets, inline, etc) but does *not* compute values
// domNode - the node to get properties for
// properties - Can be a single property to fetch or an array of properties to fetch
function getFinalStyle(domNode, properties) {
if(!(properties instanceof Array)) properties = [properties]
var parent = domNode.parentNode
if(parent) {
var originalDisplay = parent.style.display
parent.style.display = 'none'
}
var computedStyles = getComputedStyle(domNode)
var result = {}
properties.forEach(function(prop) {
result[prop] = computedStyles[prop]
})
if(parent) {
parent.style.display = originalDisplay
}
return result
}
使用交叉乘法将像素转换为百分比。
公式设置:
1.)(element_width_pixels / parent_width_pixels)=(element_width_percentage / 100)
2.)element_width_percentage =(100 * element_width_pixels)/ parent_width_pixels
实际代码:
<script>
var $width_percentage = (100 * $("#child").width()) / $("#parent").width();
</script>