javascript - 如何从另一个页面链接到 jQuery 过滤视图并选择默认过滤器?
问题描述
对不起我的知识和语言。我在 jQuery 中有两个连接的过滤器,我希望能够从其他页面链接到特定的过滤器视图。我知道我可能必须使用window.location.hash
,但我不知道如何使用。
第二个问题 - 如何添加默认过滤器?我想要ONE
并Washington
作为默认选择的过滤器。
非常感谢任何帮助
$("button").click(function() {
//remove the selected class from the previously selected button
$(this).closest('.filter').find('button.selected').removeClass('selected');
//put the class on the button you clicked
$(this).addClass('selected');
//get the data-filter off of the selected buttons and join them into a selector
var filter = '.'+ $('button.selected').map(function(){
return $(this).data('filter');
}).get().join('.');
//hide all the posts, and then show those that match
$('.post').hide().filter(filter).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>FIRST</h2>
<!-- we use a class because an id cannot be repeated
and we want to select both our filters later -->
<div class='filter'>
<!-- data fields let us separate the classes from the data that
is used for the filter -->
<button data-filter='all'>All</button>
<button data-filter='1'>One</button>
<button data-filter='2'>Two</button>
</div>
<h2>TWO</h2>
<div class='filter'>
<button data-filter='all'>All</button>
<button data-filter='aa'>Washington</button>
<button data-filter='bb'>Philadelphia</button>
</div>
<br>
<div id='posts'>
<div class='all post 1 aa'>One Washington</div>
<div class='all post 1 bb'>One Philadelphia</div>
<div class='all post 2 bb'>Two Philadelphia</div>
<div class='all post 2 aa'>Two Washington</div>
<div class='all post 1 bb'>One Philadelphia</div>
<div class='all post 2 aa'>Two Washington</div>
<div class='all post 1 bb'>One Philadelphia</div>
</div>
解决方案
推荐阅读
- javascript - 反应不重新获取过期的缓存文件
- android - Play Store 应用程序不会自动更新并出现错误:APP_IN_FOREGROUND
- unity3d - 使用 Physics 2D 拖动播放器对象
- r - R,时变因子。如何找到最接近某个时间的值?
- ruby-on-rails - 如何检查数组中是否存在值?
- azure - 使用 JSON 表处理逻辑应用中的空值并编写对象
- c++ - 使用 ASIO SDK 2.3 I/O 的任意播放软件的实时均衡器
- postgresql - 在没有互联网的情况下从 mac 连接到 docker postgresql
- c# - 更新 .NET Standard 库以使用 ASP.NET Core 2.2 和 3.0
- doctrine - 如何为 bin/console 启用学说:schema 命名空间?