admin管理员组文章数量:1316845
Following is html code of two pages where in one page i have written data-filter attribute which i need to apply in other html page. The way i have done it's not working.
<div class="grid_12" id="category-nav">
<ul id="category" class="list-of-links centered">
<li><a href="otherpage.html" class="current-cat" data-filter="rondvaart">Rondvaart</a></li>
<li><a href="otherpage.html" data-filter="wandelingen">Wandelingen</a></li>
<li><a href="otherpage.html" data-filter="rondleidingen">Rondleidingen</a></li>
<li><a href="otherpage.html" data-filter="groepsarrangementen">Groepsarrangementen</a></li>
</ul>
The HTML code for otherpage.html is
<article class="post" data-cat="wandelingen">
<header>
<p class="byline">Rondvaart</p>
<h2>Binnendieze</h2>
</header><!-- End header -->
</article><!-- End article.post -->
<article class="post" data-cat="rondleidingen">
<header>
<p class="byline">Rondvaart</p>
<h2>Binnendieze</h2>
</header><!-- End header -->
<article class="post" data-cat="wandelingen">
<header>
<p class="byline">Rondvaart</p>
<h2>Binnendieze</h2>
</header><!-- End header -->
Javascript/Jquery is:
var posts = $('.post');
posts.hide();
$( "#category li a" ).click(function() {
var customType = $( this ).data('filter');
console.log(customType);
console.log(posts.length);
posts
.hide()
.filter(function () {
return $(this).data('cat') === customType;
})
.show();
});
Following is html code of two pages where in one page i have written data-filter attribute which i need to apply in other html page. The way i have done it's not working.
<div class="grid_12" id="category-nav">
<ul id="category" class="list-of-links centered">
<li><a href="otherpage.html" class="current-cat" data-filter="rondvaart">Rondvaart</a></li>
<li><a href="otherpage.html" data-filter="wandelingen">Wandelingen</a></li>
<li><a href="otherpage.html" data-filter="rondleidingen">Rondleidingen</a></li>
<li><a href="otherpage.html" data-filter="groepsarrangementen">Groepsarrangementen</a></li>
</ul>
The HTML code for otherpage.html is
<article class="post" data-cat="wandelingen">
<header>
<p class="byline">Rondvaart</p>
<h2>Binnendieze</h2>
</header><!-- End header -->
</article><!-- End article.post -->
<article class="post" data-cat="rondleidingen">
<header>
<p class="byline">Rondvaart</p>
<h2>Binnendieze</h2>
</header><!-- End header -->
<article class="post" data-cat="wandelingen">
<header>
<p class="byline">Rondvaart</p>
<h2>Binnendieze</h2>
</header><!-- End header -->
Javascript/Jquery is:
var posts = $('.post');
posts.hide();
$( "#category li a" ).click(function() {
var customType = $( this ).data('filter');
console.log(customType);
console.log(posts.length);
posts
.hide()
.filter(function () {
return $(this).data('cat') === customType;
})
.show();
});
Share
Improve this question
edited Feb 2, 2014 at 10:59
Super Hornet
2,9075 gold badges30 silver badges58 bronze badges
asked Feb 2, 2014 at 10:49
ITGeekITGeek
451 gold badge4 silver badges12 bronze badges
6
- It's not taking data-filter's value from page where it is given?what's the problem or any alternative for above? – ITGeek Commented Feb 2, 2014 at 10:55
-
You mean when you click on a link the relative data is not storing in
customType
fromvar customType = $( this ).data('filter');
? – Suman Bogati Commented Feb 2, 2014 at 11:08 - 1 please add the actual question to your question. – xlembouras Commented Feb 2, 2014 at 11:08
- If i click on Wandelingen link,it should redirect to otherpage.html and filter but it's not happening in above code,that's actual question – ITGeek Commented Feb 2, 2014 at 11:13
- @user2696142 any ment about my answer? – Ergec Commented Feb 3, 2014 at 12:10
1 Answer
Reset to default 2Your problem is you are redirection on click to otherpage.html without any info about your data filter. Also it's way simpler to do this in one page not two. Check this fiddle out
http://jsfiddle/ergec/kwPLp/
For two page setup
First page HTML only, no javascript
<ul id="category" class="list-of-links centered">
<li><a href="otherpage.html#rondvaart" class="current-cat" data-filter="">Rondvaart</a></li>
<li><a href="otherpage.html#wandelingen" data-filter="">Wandelingen</a></li>
<li><a href="otherpage.html#rondleidingen" data-filter="">Rondleidingen</a></li>
<li><a href="otherpage.html#groepsarrangementen" data-filter="">Groepsarrangementen</a></li>
</ul>
Second Page (otherpage.html)
HTML
<article class="post" id="rondvaart">
<header>
<p class="byline">Rondvaart</p>
<h2>rondvaart</h2>
</header>
<!-- End header -->
</article>
<!-- End article.post -->
<article class="post" id="wandelingen">
<header>
<p class="byline">wandelingen</p>
<h2>wandelingen</h2>
</header>
<!-- End header -->
</article>
<!-- End article.post -->
<article class="post" id="rondleidingen">
<header>
<p class="byline">rondleidingen</p>
<h2>rondleidingen</h2>
</header>
<!-- End header -->
</article>
<!-- End article.post -->
<article class="post" id="groepsarrangementen">
<header>
<p class="byline">groepsarrangementen</p>
<h2>groepsarrangementen</h2>
</header>
<!-- End header -->
</article>
<!-- End article.post -->
Javascript
var hash = window.location.hash;
hash = hash.split("#");
hash = hash[1];
var posts = $('.post');
posts.hide();
$("#" + hash ).show();
本文标签: javascriptusing datafilter attributeStack Overflow
版权声明:本文标题:javascript - using data-filter attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742014342a2413483.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论