根据数据属性显示/隐藏Div

时间:2016-10-08 08:50:22

标签: javascript

我试图隐藏具有特定数据属性但有问题的元素。在这种情况下,我试图根据用户点击按钮隐藏优惠或优惠券。还尝试根据show all显示所有内容。

有人可以帮我解决我的错误。

此处代码

function showdeals(dealtype) {
		if (dealtype == "all") {
			$('div[data-deal-type="deal"]').show();
			$('div[data-deal-type="coupon"]').show();

		}
		if (dealtype == "deal") {
			$('div[data-deal-type="deal"]').hide();
			$('div[data-deal-type="coupon"]').show();
		}
		else if (dealtype == "coupon") {
			$('div[data-deal-type="deal"]').show();
			$('div[data-deal-type="coupon"]').hide();
		}
	}
<button onclick="showdeals('all')">
Show All 
</button>
<button onclick="showdeals('deal')">
Show Deals
</button>
<button onclick="showdeals('coupon')">
Show coupons 
</button>




<div id="536739" class="coupon-modal" data-deal-type="coupon" data-discount-type="data-coupon-rank=" xxx=""> Coupon1 </div>

<div id="536738" class="coupon-modal" data-deal-type="coupon" data-discount-type="data-coupon-rank=" xxx=""> coupon2 </div>

<div id="536737" class="coupon-modal" data-deal-type="deal" data-discount-type="data-coupon-rank=" xxx=""> Deal1 </div>

<div id="536736" class="coupon-modal" data-deal-type="deal" data-discount-type="data-coupon-rank=" xxx=""> deal2 </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 个答案:

答案 0 :(得分:1)

你的条件落后了。你应该展示它们时隐藏交易,反之亦然,优惠券也是如此。

if (dealtype == "all") {
    $('div[data-deal-type="deal"]').show();
    $('div[data-deal-type="coupon"]').show();
}
if (dealtype == "deal") {
    $('div[data-deal-type="deal"]').hide();       // Should show
    $('div[data-deal-type="coupon"]').show();     // Should hide
}
else if (dealtype == "coupon") {
    $('div[data-deal-type="deal"]').show();       // Should hide
    $('div[data-deal-type="coupon"]').hide();     // Should show
}

(你可以使用另一个else,但这并不重要。)

那就是说,你可以更简单地做到:

function showdeals(dealtype) {
  $('div[data-deal-type="deal"]').toggle(
    dealtype == "all" || dealtype == "deal"
  );
  $('div[data-deal-type="coupon"]').toggle(
    dealtype == "all" || dealtype == "coupon"
  );
}

function showdeals(dealtype) {
  $('div[data-deal-type="deal"]').toggle(
    dealtype == "all" || dealtype == "deal"
  );
  $('div[data-deal-type="coupon"]').toggle(
    dealtype == "all" || dealtype == "coupon"
  );
}
<button onclick="showdeals('all')">
  Show All
</button>
<button onclick="showdeals('deal')">
  Show Deals
</button>
<button onclick="showdeals('coupon')">
  Show coupons
</button>




<div id="536739" class="coupon-modal" data-deal-type="coupon" data-discount-type="data-coupon-rank=" xxx="">Coupon1</div>

<div id="536738" class="coupon-modal" data-deal-type="coupon" data-discount-type="data-coupon-rank=" xxx="">coupon2</div>

<div id="536737" class="coupon-modal" data-deal-type="deal" data-discount-type="data-coupon-rank=" xxx="">Deal1</div>

<div id="536736" class="coupon-modal" data-deal-type="deal" data-discount-type="data-coupon-rank=" xxx="">deal2</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>