如何将类添加到span元素?
即时class="fa fa-close"
我需要使用jQuery-按ID(即id="sec2"
)然后按属性(即data-id="attrac-24"
)找到“ sec”
HTML示例:
<div class="well dropZone" id="sec2">
<div class="panel panel-info attrac ui-widget-content" data-id="attrac-24" >
<div class="panel-heading"><?PHP echo mb_substr ($attracIndex['title'], 0, 45, "utf-8") ?> <span class="pull-right"><i class="fa"></i></span></div>
<div class="panel-body"><?PHP echo mb_substr ($attracIndex['des'], 0, 120, "utf-8")?></div>
</div>
</div>
<div class="well dropZone" id="sec3">
<div class="panel panel-info attrac ui-widget-content" data-id="attrac-24" >
<div class="panel-heading"><?PHP echo mb_substr ($attracIndex['title'], 0, 45, "utf-8") ?> <span class="pull-right"><i class="fa"></i></span></div>
<div class="panel-body"><?PHP echo mb_substr ($attracIndex['des'], 0, 120, "utf-8")?></div>
</div>
<div class="panel panel-info attrac ui-widget-content" data-id="attrac-35" >
<div class="panel-heading"><?PHP echo mb_substr ($attracIndex['title'], 0, 45, "utf-8") ?> <span class="pull-right"><i class="fa"></i></span></div>
<div class="panel-body"><?PHP echo mb_substr ($attracIndex['des'], 0, 120, "utf-8")?></div>
</div>
</div>
JQUERY
var secID = event.target.id;
var attacID = ui.item.data('id');
我尝试过:
$("#"+secID + " [data-id='"+attacID+"']" ).find("fa").addClass("fa-close");
答案 0 :(得分:0)
选择器正确,但是.find("fa")
错误。 fa
是一门课程,因此应为.find(".fa")
试试这个:
$("#"+secID + " [data-id='"+attacID+"']" ).find(".fa").addClass("fa-close");
这是一个jsfiddle:https://jsfiddle.net/leonardo9717/xpvt214o/262447/