我正在使用JavaScript / jQuery来检测浏览器是否支持nth-of-type
CSS3伪类。在CSS中分配了5px padding-left
(参见下面的代码),如果JS / jQ可以通过$(document).ready()
读取它,则检测成功。
Chrome 26,IE9,Win Safari 5.1和Opera 12都能正常运行,所有这些都能检测出5px设置。但是,确实支持nth-of-type
的Firefox 20将padding-left
设置显示为0px,因此测试错误。为什么nth-of-type
检测仅在Firefox中失败?
示例代码。 (从我的生产代码中删除。如果您想要试用它,也可以在http://jsfiddle.net/jma7777/ZnzBN/1/使用。打开控制台以查看padding-left
检测结果。)
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Pseudo Class Nth-of-Type Detector</title>
<style>
#checkPsdoClass1 tr.rows:nth-of-type(odd) {
padding-left: 5px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
// Checks for style assigned to :nth-of-type(odd)
if ($('#checkPsdoClass2').css('padding-left') !== '5px') {
$('#pseudoClassDemo').html('<p>Your browser does not support the <code>:nth-of-type</code> pseudo-class.');
}
console.log($('#checkPsdoClass2').css('padding-left')); // Modern browsers log 5px except Firefox which logs 0px
console.log($('#checkPsdoClass1 tr.rows:nth-of-type(odd)').css('padding-left')); // Modern browsers log 5px except Firefox which logs 0px
console.log($('tr.rows:nth-of-type(odd)').css('padding-left')); // Modern browsers log 5px except Firefox which logs 0px
});
</script>
</head>
<body>
<table id="checkPsdoClass1"><tr id="checkPsdoClass2" class="rows"><td><p id="checkPdsoElmnt">This table is used to test if the browser supports the nth-of-type pseudo-class.</p></td></tr></table>
</body>
</html>
答案 0 :(得分:3)
我认为问题是Firefox不对表行元素应用填充;有关详细信息,请参阅MDN:
[padding]适用于除table-row-group,table-header-group,table-footer-group,table-row,table-column-group和table-column
之外的所有元素
尝试将填充分配给第一个子单元格:
#checkPsdoClass1 tr.rows:nth-of-type(odd) td:first-child {
padding-left: 5px;
}