[英]jQuery check if CSS width is greater than 0 then add CSS property
我在这里一定做错了。 我希望jQuery检查CSS元素的宽度是否大于0,然后添加CSS属性:
<script>
$(document).ready(function(){
$(".neo-P4EMilestonesMilestone").each(function() {
$(this).css('width') !=0) {
$(this).css("display", "block");
});
});
});
</script>
class元素的默认宽度为0:
<div class="neo-P4EMilestonesMilestone" style="width:0">
我希望jQuery查看任何.neo-P4EMilestonesMilestone类,如果宽度未设置为“ 0”,则添加“ display:block”。 像这样:
<div class="neo-P4EMilestonesMilestone" style="width:10%;display:block;">
您可以将其简化为以下内容:
$('.neo-P4EMilestonesMilestone').each(function () {
$(this).css('display', $(this).width() > 0 ? 'block' : '');
});
您的代码段无法正常运行有几个原因。 首先,存在语法错误。 另外,当您通过$(this).css('width')
检索元素的宽度时,会包含px
单位。 您可能必须分析字符串parseInt($(this).css('width'), 10)
才能检查它是否大于0
。 不必这样做,您只需使用$(this).width()
检索宽度,然后就没有必要了。
Yuu有一些语法错误。 否, if
关键字,没有大括号。 见下文。
$(".neo-P4EMilestonesMilestone").each(function () {
if ($(this).width() != 0) {
$(this).css({
'display': 'block',
'width': '10 % '
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.