if else语句检查div类是否存在

时间:2014-05-13 10:00:07

标签: jquery if-statement

的Jquery HTML

我想要一个if else语句,检查类.post-single是否存在 然后如果excist执行代码:

single = $('.post-single').height();
tab = $('#tab');
tab_height = tab.height();
footer_height = $('footer').height();
max_top = (single + 323) - (tab_height);
tab_offset = tab.offset();

如果不是,则执行相同的代码,但执行不同的类

single = $('.wrap content').height();
tab = $('#tab');
tab_height = tab.height();
footer_height = $('footer').height();
max_top = (single + 323) - (tab_height);
tab_offset = tab.offset();

2 个答案:

答案 0 :(得分:1)

尝试使用元素的length属性来检查其存在,

if($('.post-single').length) {
 //exist
}
else {
 //not exist
}

完整代码,

single =($('.post-single').length) ? $('.post-single').height() : $('.wrap content').height();  
tab = $('#tab');
tab_height = tab.height();
footer_height = $('footer').height();
max_top = (single + 323) - (tab_height);
tab_offset = tab.offset();

答案 1 :(得分:0)

使用如下所示的长度属性

if($(".post-single").length > 0)
  alert(".post-single exist");
相关问题