我想在单击“活动”和“不活动”时更改(i)标签的颜色。
此角色运行成功,但是当我单击任何(a)标签时,也首先更改(a)标签颜色。
我的CSS
.active{color:green;}
.deactive{color:red;}
我的Ajax代码
$('.pactive').click(function(){
var pactiveId = $(this).attr('id');
var currentVal = $(this).find('i').attr('title');
var addCls = 'active';
var removeCls = 'deactive';
var title = 'Active';
if(currentVal == 'Active'){
addCls = 'deactive';
removeCls = 'active';
title = 'Deactive';
}
$(this).find('i').addClass(addCls).removeClass(removeCls);
$(this).find('i').attr('title',title);
$.post('controller/ajax-product-active.php?pactiveId='+pactiveId,
{},function(data)
{
$('#product-active').html(data);
});
});
ajax-product-active.php
$pactiveId = $_GET['pactiveId'];
$checkstatus = mysqli_query($conn,"select * from products where id = '{$pactiveId}'");
while($prow = mysqli_fetch_array($checkstatus))
{
if ($prow['status']=='Active') {
$update_status = mysqli_query($conn,"update products set status = 'Deactive' where id = '{$pactiveId}'");
echo "<a id='$prow[id]' class='pactive' style='cursor: pointer;'>
<i class='fa fa-circle active' aria-hidden='true' title='Active'></i></a>";
}
elseif ($prow['status']=='Deactive') {
$update_status = mysqli_query($conn,"update products set status = 'Active' where id = '{$pactiveId}'");
echo "<a id='$prow[id]' class='pactive' style='cursor: pointer;'>
<i class='fa fa-circle deactive' aria-hidden='true' title='Deactive'></i></a>";
}
}
此代码成功运行,但是当我单击任何(a)标签时,也会在第一个(a)标签上更改颜色。 我只想在单击(a)标签时更改此(a)标签的颜色。 请帮助
答案 0 :(得分:0)
我认为您需要在anchor
标记中添加类,而不是像下面这样的i
:-
jQuery:-
$(this).find('a').addClass(addCls).removeClass(removeCls);
PHP:-
echo "<a id='$prow[id]' class='pactive active' style='cursor: pointer;'>
<i class='fa fa-circle' aria-hidden='true' title='Active'></i></a>";