Php foreach循环生成的每个按钮的Jquery特定操作
如何为foreach循环生成的每个按钮创建特定的Jquery操作Php foreach循环生成的每个按钮的Jquery特定操作,php,jquery,Php,Jquery,如何为foreach循环生成的每个按钮创建特定的Jquery操作 $(文档).ready(函数(){ $(“#numero”).hide(); $(“#shownum”)。单击(函数(){ $(“#numero”).toggle(); }); }); 显示/隐藏 您的jQuery代码选择一个ID,整个站点的ID应该是唯一的。因此,您不能在标记中使用id属性。或者添加一个自定义CSS类 <button ... class="... my-button" $(".my-button"
$(文档).ready(函数(){
$(“#numero”).hide();
$(“#shownum”)。单击(函数(){
$(“#numero”).toggle();
});
});
显示/隐藏
您的jQuery代码选择一个ID,整个站点的ID应该是唯一的。因此,您不能在
标记中使用id属性。或者添加一个自定义CSS类
<button ... class="... my-button"
$(".my-button").click(...
您的jQuery代码选择一个ID,并且ID在整个站点上应该是唯一的。因此,您不能在
标记中使用id属性。或者添加一个自定义CSS类
<button ... class="... my-button"
$(".my-button").click(...
在php foreach中,您可以生成按钮,以便id为:
在php foreach中,您可以生成按钮,以便id为:
您不需要使用任何数据属性
或将其与id
过度复杂化,而是应该使用类
而不是使用id
,并使用jquery遍历正确选择元素
<?php foreach ($annonces as $key => $annonce) : ?>
<button type="button" class="btn btn-primary btn-xs shownum_class"> <span class=" glyphicon glyphicon-earphone" aria-hidden="true"> </span> show/hide</button>
<div class="numero_class">
<?php echo $annonce['Annonce']['title'] ;?>
</div>
<?php endforeach; ?>
单击shownum_class
,它将选择下一个特定于每个按钮的元素numo_class
,然后将其切换。您无需使用任何数据属性或将其与id
过度复杂化,相反,您应该使用class
而不是id
,并使用jquery遍历正确地选择元素
<?php foreach ($annonces as $key => $annonce) : ?>
<button type="button" class="btn btn-primary btn-xs shownum_class"> <span class=" glyphicon glyphicon-earphone" aria-hidden="true"> </span> show/hide</button>
<div class="numero_class">
<?php echo $annonce['Annonce']['title'] ;?>
</div>
<?php endforeach; ?>
单击shownum_class
,它将选择下一个特定于每个按钮的元素
numero_class
,然后切换它。尝试以下操作:
php和html代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<?php $i = 0; foreach ($annonces as $key => $annonce) : ?>
<button type="button" class="btn btn-primary btn-xs" class="shownum" data-target = <?php echo"'".$i."'"; ?>> <span class=" glyphicon glyphicon-earphone" aria-hidden="true"> </span> show/hide</button>
<div id= <?php echo "numero".$i; ?> class="hideOnLoad" >
<?php echo $annonce['Annonce']['title'] ; $i++;?>
</div>
<?php endforeach; ?>
javascript:
<script type="text/javascript">
$(document).ready(function() {
$(".hideOnLoad").hide();
$(".shownum").click(function(){
$("#numero"+ $(this).data('target')).toggle();
});
});
</script>
$(文档).ready(函数(){
$(“.hideOnLoad”).hide();
$(“.shownum”)。单击(函数(){
$(“#numero”+$(this.data('target')).toggle();
});
});
试试这个:
php和html代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<?php $i = 0; foreach ($annonces as $key => $annonce) : ?>
<button type="button" class="btn btn-primary btn-xs" class="shownum" data-target = <?php echo"'".$i."'"; ?>> <span class=" glyphicon glyphicon-earphone" aria-hidden="true"> </span> show/hide</button>
<div id= <?php echo "numero".$i; ?> class="hideOnLoad" >
<?php echo $annonce['Annonce']['title'] ; $i++;?>
</div>
<?php endforeach; ?>
javascript:
<script type="text/javascript">
$(document).ready(function() {
$(".hideOnLoad").hide();
$(".shownum").click(function(){
$("#numero"+ $(this).data('target')).toggle();
});
});
</script>
$(文档).ready(函数(){
$(“.hideOnLoad”).hide();
$(“.shownum”)。单击(函数(){
$(“#numero”+$(this.data('target')).toggle();
});
});
试试这个:-
<?php foreach ($annonces as $key => $annonce) : ?>
<button type="button" class="btn btn-primary btn-xs shownum">
<span class=" glyphicon glyphicon-earphone" aria-hidden="true">show/hide</spavn>
</button>
<div class="numero">
<?php echo echo $annonce['Annonce']['title'] ;?>
</div>
<?php endforeach; ?>
显示/隐藏
Js:-
(功能(){
$(“div.numero”).hide();
$(“button.shownum”)。单击(函数(){
var$this=$(this.next('div.numero');
$this.toggle();
返回false;
});
})();
试试这个:-
<?php foreach ($annonces as $key => $annonce) : ?>
<button type="button" class="btn btn-primary btn-xs shownum">
<span class=" glyphicon glyphicon-earphone" aria-hidden="true">show/hide</spavn>
</button>
<div class="numero">
<?php echo echo $annonce['Annonce']['title'] ;?>
</div>
<?php endforeach; ?>
显示/隐藏
Js:-
(功能(){
$(“div.numero”).hide();
$(“button.shownum”)。单击(函数(){
var$this=$(this.next('div.numero');
$this.toggle();
返回false;
});
})();
您应该使用class属性而不是id属性,并将整个内容包装在容器中
<?php foreach ($annonces as $key => $annonce) : ?>
<div class="wrapper">
<button type="button" class="btn btn-primary btn-xs show-title" > <span class=" glyphicon glyphicon-earphone" aria-hidden="true"> </span> show/hide</button>
<div class="title-content">
<?php echo $annonce['Annonce']['title'] ;?>
</div>
</div>
<?php endforeach; ?>
显示/隐藏
下面是用于切换的jquery代码
<script type="text/javascript">
$(document).ready(function() {
$(".title-content").hide();
$(".show-title").click(function(){
$(this).parent(".wrapper").find(".title-content").toggle();
});
});
</script>
$(文档).ready(函数(){
$(“.title content”).hide();
$(“.show title”)。单击(函数(){
$(this.parent(“.wrapper”).find(“.title content”).toggle();
});
});
您应该使用class属性而不是id属性,并将整个内容包装在容器中
<?php foreach ($annonces as $key => $annonce) : ?>
<div class="wrapper">
<button type="button" class="btn btn-primary btn-xs show-title" > <span class=" glyphicon glyphicon-earphone" aria-hidden="true"> </span> show/hide</button>
<div class="title-content">
<?php echo $annonce['Annonce']['title'] ;?>
</div>
</div>
<?php endforeach; ?>
显示/隐藏
下面是用于切换的jquery代码
<script type="text/javascript">
$(document).ready(function() {
$(".title-content").hide();
$(".show-title").click(function(){
$(this).parent(".wrapper").find(".title-content").toggle();
});
});
</script>
$(文档).ready(函数(){
$(“.title content”).hide();
$(“.show title”)。单击(函数(){
$(this.parent(“.wrapper”).find(“.title content”).toggle();
});
});
改用类。改用类。