为什么这个警告是“未定义的”而不是链接id的?我确实得到了5次“未定义”,所以我知道它正确地在它们上循环,但是它没有显示名称,比如'Cripple‘等等。
$(function () {
$("#divPlayerActions").children("a").each(function () {
var e = $(this);
alert(e.id); // undefined ?
});
}<div id="divPlayerActions" class="col-md-6 text-center">
<a href="#" class="action" data-cd="0" data-cast="0" id='Cripple'> <img src="/Content/cripple.png" /> </a>
<a href="#" class="action" data-cd="0" data-cast="0" id='GroundSlam'> <img src="/Content/ground_slam.png" /> </a>
<a href="#" class="action" data-cd="0" data-cast="0" id='HealingStar'> <img src="/Content/healing_star.png" /> </a>
<a href="#" class="action" data-cd="0" data-cast="0" id='LighteningStrike'> <img src="/Content/lightening_strike.png" /> </a>
<a href="#" class="action" data-cd="0" data-cast="0" id='PoisonBite'> <img src="/Content/poison_bite.png" /> </a>
</div>发布于 2014-12-31 01:51:01
您需要将本机JS元素对象从jQuery对象中撤消:
alert( e[0].id );或者简单地做
alert( this.id );或者使用jQuery:
alert( e.prop("id") ); /* this one */
alert( e.get(0).id; ); /* or this one */
alert( e.attr("id") ); /* or this one */或者通过this .each(index, element)参数传递jQuery目标标识符:
$("#divPlayerActions").children("a").each(function (i, e) {
alert(e.id); // Cripple // GroundSlam // HealingStar .....
});发布于 2014-12-31 01:53:18
如果您想通过jQuery获取这个值,只需将其作为一个属性来获取:
e.attr('id');发布于 2014-12-31 03:00:37
尝试使用此代码检索元素ID
$(function () {
$("#divPlayerActions").children("a").each(function () {
var e = $(this);
alert( e.prop("id"));
});
}https://stackoverflow.com/questions/27715155
复制相似问题