我有一个免责声明div,我想在点击某个菜单项时显示。我让我的Jquery用这个脚本正确显示我隐藏的div:
$(function(){
var $allItems = $(".food-container > div");
$(document.body).on("click", "a.menu-item", function () {
var id = this.id, itemId = ".food-container > #item-" + id;
$allItems.not($(itemId).fadeToggle()).hide();
});
});
我正在使用高级自定义字段转发器字段,因此我需要将字段隐藏在Repeater循环之外。我拥有的菜单ID是" 4"。所以我只需要连接免责声明div,即" burger-disclaimer" #4点击事件。
答案 0 :(得分:1)
这应该让你走上正确的道路。我正在抓取click事件的ID(evt变量),以查看所选内容。
HTML:
<a id="1" href="javascript:void(0)" class="menu-item">Appetizers<i class="fa fa-caret-right"></i></a>
<a id="2" href="javascript:void(0)" class="menu-item">Tex-Mex<i class="fa fa-caret-right"></i></a>
<a id="3" href="javascript:void(0)" class="menu-item">Salads<i class="fa fa-caret-right"></i></a>
<a id="4" href="javascript:void(0)" class="menu-item">Burgers<i class="fa fa-caret-right"></i></a>
<div class="food-container">
<div id="item-1" style="display: none;">
<div class="food">
<h3>Mac N Cheese Bites</h3> Breaded, then fried macaroni, stuffed with cheese</div>
<div class="price"> $8 </div>
</div>
<div id="item-2" style="display: none;">
<div class="food">
<h3>Salads</h3> Salads
</div>
<div class="price"> $8 </div>
</div>
<div id="item-3" style="display: none;">
<div class="food">
<h3>Tex-Mex</h3> Tex-Mex
</div>
<div class="price"> $8 </div>
</div>
<div id="item-4" style="display: none;">
<div class="food">
<h3>Burgers</h3> Burger
</div>
<div class="price"> $8 </div>
</div>
</div>
<div class="burger-disclaimer">
*All sandwiches and burgers are served with fries or potato chips. Side salad and onion rings available as side, add $2 upcharge
</div>
JS:
$(function() {
var $allItems = $(".food-container > div");
$(document.body).on("click", "a.menu-item", function(evt) {
var id = this.id,
itemId = ".food-container > #item-" + id;
$allItems.not($(itemId).fadeToggle()).hide();
if (evt.target.id == 4) {
$('.burger-disclaimer').toggle();
} else {
$('.burger-disclaimer').hide();
}
});
});
你更新的JS小提琴:https://jsfiddle.net/6y0ymao8/