我想到了。您可以在此处查看代码。
function px_to_em(px_value, container) {
////////////////////////////////
// Detect Contianer Font Size //
////////////////////////////////
//Prepend a span tag to the container for testing
$(container).prepend("<span id='wraping_span_to_test'></span>");
//Set the span to height 1em and then save the pixel height to the container_ft_sz variable
container_ft_size = $("#wraping_span_to_test").css({
"font-family": "inherit",
"font-size": "inherit",
"font-style": "inherit",
"font-variant": "inherit",
"font-weight": "inherit",
"line-height": "inherit",
"margin": "0 !important",
"padding": "0 !important",
"border": "0 !important",
"outline": "0 !important",
"background-color": "aqua",
"height": "1em",
"width": " 1em",
"display": "block"
}).height();
//Remove the testing span
$("#wraping_span_to_test").remove();
//Use values to convert to ems
var round_to = 1000;
return Math.round((px_value / container_ft_size) * round_to) / round_to;
}
这应该允许您使用 jQuery 找到按像素测量的测量值,并通过调用一个简单的函数将它们转换为 em 测量值。