[英]Jquery Show Hide with attribute
How to get value from custom attribute to be used in if else condition? 如何从自定义属性中获取要在其他情况下使用的值? I want to switch button between show & hide . 我想在显示和隐藏之间切换按钮。 if show button clicked it will hiden and the hide button showed. 如果单击显示按钮,它将隐藏并显示隐藏按钮。 And also the same for opposites. 对立面也一样。 So i can do show hide for my divs. 所以我可以为我的div显示隐藏。
Here's my codes 这是我的密码
<div class="wrapper">
<a class="show_detail" target="1" style="display:block">+ Show</a>
<a class="hide_detail" target-hide="1" style="display:none">- Hide</a>
<div class="event_down" id="event_down1" style="display:none">
Content 1
</div>
<a class="show_detail" target="2" style="display:block">+ Show</a>
<a class="hide_detail" target-hide="2" style="display:none">- Hide</a>
<div class="event_down" id="event_down2" style="display:none">
Content 2
</div>
<a class="show_detail" target="3" style="display:block">+ Show</a>
<a class="hide_detail" target-hide="3" style="display:none">- Hide</a>
<div class="event_down" id="event_down3" style="display:none">
Content 3
</div>
</div>
CSS: CSS:
.show_detail{cursor:pointer; color:red;}
.hide_detail{cursor:pointer; color:red;}
JS : JS:
$('.show_detail').click(function(){
var atribut_show = $('.show_detail').attr('target');
var atribut_hide = $('.hide_detail').attr('target-hide');
if (atribut_show == atribut_hide){
$('.hide_detail').show();
$(this).hide();
}
$('.event_down').hide();
$('#event_down'+$(this).attr('target')).show();
});
and here's MY FIDDLE need your help to solve it. 这是我的兄弟需要您的帮助来解决。
in order to get custom attributes their name must start with "data-". 为了获得自定义属性,其名称必须以“ data-”开头。 For example your custom attribute target would be "data-target". 例如,您的自定义属性目标将是“数据目标”。 After that you can get them using something like $("#myElement").getAttribute("data-target"). 之后,您可以使用$(“#myElement”)。getAttribute(“ data-target”)之类的方法获取它们。
The following javascript made it function for me. 以下javascript使其对我有用。 You should however consider calling your attributes data-target
and data-target-hide
as your specified attributes are not actually valid. 但是,由于指定的属性实际上无效,因此应考虑将属性称为data-target
和data-target-hide
。 It will function, but you could run into problems if you don't change the attribute names. 它将起作用,但是如果不更改属性名称,可能会遇到问题。
$('.show_detail').click(function(){
var atribut_show = $(this).attr('target');
$('.hide_detail[target-hide="'+atribut_show+'"]').show();
$(this).hide();
$('#event_down'+atribut_show).show();
});
$('.hide_detail').click(function(){
var atribut_hide = $(this).attr('target-hide');
$('.show_detail[target="'+atribut_hide+'"]').show();
$(this).hide();
$('#event_down'+atribut_hide).hide();
});
You are getting object array list you have to get only the current object Check updated fiddle here 您正在获取对象数组列表,您只需要获取当前对象,请在此处检查更新的小提琴
" http://jsfiddle.net/p7Krf/3/ " “ http://jsfiddle.net/p7Krf/3/ ”
$('.show_detail').click(function(){
var atribut_show = $(this).attr('target');
$('.hide_detail').each(function(element){
if($(this).attr("target-hide")==atribut_show){
$(this).show();
}
});
$(this).hide();
$('#event_down'+atribut_show).show();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.