仅使用JavaScript验证可见选项卡

时间:2014-05-06 07:48:49

标签: javascript validation

我使用VB.NET IF语句设置三个可见或不可见的网站选项卡。

我需要一种方法来确保仅当选项卡可见时才会执行这些选项卡上的控件的JavaScript验证代码。我想,验证(if语句的纠结)必须进入另一个if。

这样的东西
if (code here to check whether tabs are visible) {
    if { validation takes place here }
}

有人可以告诉我如何使用JavaScript来检查我的标签是否可见?

1 个答案:

答案 0 :(得分:0)

我不知道.NET。所以我将假设这个解决方案的一些内容,包括你正在使用jquery:

$('.tab:visible').each(function(index, tab){
   // run vals on $(tab) or tab
});

或者,如果您已经在每个标签的循环中:

if($(tab).is(':visible')){
 // validate(tab)
}

------没有jQuery ------

让我们假设您将类hidden添加到选项卡以隐藏它。 html(简化)可能如下所示:

<body>
  <div class='tab'>Hello</div>
  <div class='tab'>World</div>
  <div class='tab hidden'>Mind Blown</div>
</body>

然后JS:

var tab, tabs, _i, _len;

// Get an array of elements with class `tab`
tabs = document.getElementsByClassName('tab');

// Loop through the array of tabs and assign `tab` to be current tab
for (_i = 0, _len = tabs.length; _i < _len; _i++) {
  tab = tabs[_i];
  // Check if `tab` has the class `hidden` in which case we will NOT perform our check
  if (tab.className.indexOf('hidden') === -1) {
    // Perform the check
    console.log('do check');
  }
}
相关问题