简体   繁体   English

通过数组迭代获得数据可见性-JavaScript

[英]data visibility by array iteration - javascript

i have html-list with states.. all "products" class stands for a product with places.. (thats the light view). 我有状态的html列表。所有“产品”类都代表具有位置的产品。(多数民众赞成在浅视图中)。 it looks like: 看起来像:

 <!-- product 1 -->
<div class="products" data-state="texas cali">
    <div class="product" data-state="texas"> ... </div>
    <div class="product" data-state="cali"> ... </div>
</div>
<!-- product 2 -->
<div class="products" data-state="utha arizona florida">
    <div class="product" data-state="utha "> ... </div>
    <div class="product" data-state="arizona"> ... </div>
    <div class="product" data-state="florida"> ... </div>
</div>

and a javascript function. 和一个javascript函数。 this function works by one state (state = 'cali'). 此功能按一种状态工作(状态='cali')。 and all div´s with 'cali' going visible. 并显示所有带有“ cali”的div。

jQuery( ".products[data-state~='" + state + "']" ).fadeIn(300, function(){
    jQuery( ".product[data-state~='" + state +"']" ).show();
});

but how it works by a array-iteration of more than one states. 但是它是如何通过多个状态的数组迭代来工作的。

var stateList = new Array("florida","utha","texas");

so all states from the array stateList should be visible. 因此数组stateList中的所有状态都应该可见。 thank you very much. 非常感谢你。

The easiest way would be to iterate through the array and apply the jquery fadeIn function. 最简单的方法是遍历数组并应用jquery fadeIn函数。

for (var i = 0; i < stateList.length; ++ i) {
  var state = stateList[i];
  jQuery( ".products[data-state~='" + state + "']" ).fadeIn(300, function(){
    jQuery( ".product[data-state~='" + state +"']" ).show();
  });
}

Something like this? 像这样吗

jQuery.each(stateList, function () {
  var state = this; // Cache the state string.
  jQuery( ".products[data-state~='" + state + "']" ).fadeIn(300, function(){
    jQuery( ".product[data-state~='" + state +"']" ).show();
  });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM