我有这张桌子:
<?php while($resulat = $sql_adherent->fetch_object()): ?>
<tr>
<td class="highlight">
<div class="important"></div>
<a href="#"><?php echo $resulat->nom_compte; ?></a>
</td>
<td class="hidden-xs"><?php echo $resulat->cin; ?></td>
<td class="hidden-xs"><?php echo $resulat->sexe; ?></td>
<td><?php echo $resulat->date_naissance; ?></td>
<td></td>
<td><?php echo $resulat->date_effet; ?></td>
<td><?php echo $resulat->date_expiration; ?></td>
<td><a href="#" class="btn default btn-xs red-stripe" id="validate">Validate</a></td>
</tr>
我想将“验证”改为“验证”,点击id=“验证”,我使用live()函数来完成这个任务,但它不起作用。
下面是我使用的代码:
$("#validate").live('click', function(e){
e.preventDefault();
$(this).addClass('green-stripe');
$(this).removeClass('red-stripe');
$(this).html('Validated');
});
我试图将代码更改为切换“验证”和“验证”,
$("#validate").toggle(function(e){
e.preventDefault();
$(this).addClass('green-stripe');
$(this).removeClass('red-stripe');
$(this).html('Validated');
}, function(){
$(this).addClass('red-stripe');
$(this).removeClass('green-stripe');
$(this).html('Validate');
});
但也没用..。我知道我做错了什么,但找不到错误。
发布于 2014-03-31 12:48:14
live
是不推荐的,所以不要再尝试使用它,而是使用on
。
toggle
事件不再被用作单击处理程序,在这种情况下,您可以检查元素是否有类作为标识元素。
参考:
注意:此方法签名在jQuery 1.8中被废弃,在jQuery 1.9中被删除。jQuery还提供了一个名为.toggle()的动画方法,用于切换元素的可见性。是否触发动画或事件方法取决于传递的参数集。
要切换类,可以将toggleClass
与多个类一起使用。
代码:
$("#validate").on('click', function (e) {
e.preventDefault();
$(this).toggleClass('red-stripe green-stripe');
($(this).hasClass('red-stripe')) ? $(this).html('Validate') : $(this).html('Validated');
});
演示:http://jsfiddle.net/IrvinDominin/VyD6x/
发布于 2014-03-31 12:45:57
.live
被否决了。把你的js换成这个。
$(document).on('click', '#validate', function(e){
e.preventDefault();
$(this).addClass('green-stripe');
$(this).removeClass('red-stripe');
$(this).html('Validated');
});
https://stackoverflow.com/questions/22761802
复制相似问题