简体   繁体   English

使用data属性自动触发页面加载点击-jQuery

[英]Automatically trigger click on page load using data attribute - jquery

I am trying to trigger a click on page load based on the data attribute, in this case data-filter. 我试图基于data属性(在本例中为data-filter)触发点击页面加载。 I have the value of the attribute saved in local storage. 我将属性的值保存在本地存储中。 I retrieve it, but I'm not sure how I target the particular list item which has that attribute. 我检索了它,但是我不确定如何定位具有该属性的特定列表项。

For example, how do I target if the local storage returns .portfolio-category-1? 例如,如何确定本地存储是否返回.portfolio-category-1?

Thanks in advance 提前致谢

 /* on click do this */ $(".edgtf-pl-filter").click(function() { var isFilter = $(this).data("filter"); localStorage.setItem('pod-filter', isFilter); alert(isFilter); }); /* On page load, do this */ var isFilterRet = localStorage.getItem('pod-filter'); setTimeout(function() { $(".edgtf-pl-filter").trigger('click'); }, 10); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="edgtf-pl-filter" data-filter=""> <span>all</span> </li> <li class="edgtf-pl-filter" data-filter=".portfolio-category-1"> <span>Categroy 1</span> </li> <li class="edgtf-pl-filter" data-filter=".portfolio-category-2"> <span>Category 2</span> </li> <li class="edgtf-pl-filter" data-filter=".portfolio-category-3"> <span>Category 3</span> </li> <li class="edgtf-pl-filter" data-filter=".portfolio-category-4"> <span>Category 4</span> </li> </ul> 

I'm not sure if I correct understand what do you what. 我不确定我是否正确理解您的意思。 You just what to trigger on element which name/class is stored in variable isFilterRet? 您只是要在元素上触发什么名称/类存储在变量isFilterRet中?

Try this: 尝试这个:

var isFilterRet = localStorage.getItem('pod-filter');
setTimeout(function() {
    if (isFilterRet){
        $(isFilterRet).trigger('click');
    }
},10);

This will work for you: 这将为您工作:

I have added a .each() function to check all .edgtf-pl-filter and identify the element with the localStorage and trigger a click() to it. 我添加了一个.each()函数来检查所有.edgtf-pl-filter并使用localStorage标识该元素并触发click()

I have removed the setTimeout() function as it is not required 我删除了setTimeout()函数,因为它不是必需的

 /* on click do this */ $(".edgtf-pl-filter").click(function() { var isFilter = $(this).data("filter"); localStorage.setItem('pod-filter', isFilter); alert(isFilter); }); /* On page load, do this */ var isFilterRet = localStorage.getItem('pod-filter'); $(".edgtf-pl-filter").each(function() { var atrFil = $(this).data("filter"); if (atrFil == isFilterRet) { $(this).trigger('click'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="edgtf-pl-filter" data-filter=""> <span>all</span> </li> <li class="edgtf-pl-filter" data-filter=".portfolio-category-1"> <span>Categroy 1</span> </li> <li class="edgtf-pl-filter" data-filter=".portfolio-category-2"> <span>Category 2</span> </li> <li class="edgtf-pl-filter" data-filter=".portfolio-category-3"> <span>Category 3</span> </li> <li class="edgtf-pl-filter" data-filter=".portfolio-category-4"> <span>Category 4</span> </li> </ul> 

working fiddle to check it's working fine. 工作提琴以检查它是否工作正常。

Hope this was helpful for you. 希望这对您有所帮助。

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

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