我正在制作一个Jquery Mobile应用程序,并有一个包含15个左右的带有class的div的页面,我正在尝试创建一个过滤系统,以便当您按下按钮时,这些div的某些消失取决于类。有3个小组,他们都有一个“全部”类来显示所有内容,总共4个类。
不幸的是,即使我将jQuery的jsfiddle设置到我的应用程序中时,我使用的大多数js都无法正常工作。
我想用
function show(target) {
document.getElementsByClassName(target).style.display = 'block';
}
function hide(target) {
document.getElementsByClassName(target).style.display = 'none';
}
但这是行不通的,而document.getElementById
似乎工作得很好。但是显然我只能隐藏/显示每个按钮1格。
我想知道是否可以解决此问题,还是应该尝试一些完全不同的方法?
这是我所拥有的jsfiddle:http : //jsfiddle.net/tzcx7gky/在jsfiddle中已完全损坏,但在我的代码中工作正常,这很奇怪。
你不需要单独的hide
-show
功能。以下内容将解决所有问题。
$('a').on("click",function()
{
$("#scroll_content div").hide(); // initially hide all divs inside the element with id = scroll_content
var target = $(this).text().toLowerCase(); // find the class you wish to show using the text of the a tag clicked ( I would prefer using data-attr in this case but I'll leave the choice upto you.
$("." + target).show(); // show the element with the class target.
});
工作示例:http : //jsfiddle.net/tzcx7gky/2/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句