所以我有一些 HTML。
<td class="qDescription">
<div>
<div id="questionTitle">% of users who logged in per day</div>
<div id="timeRangeExt"> 05 Mar 2015 - 11 Mar 2015</div>
<div class="compareToLabel"> (26 Feb 2015 - 04 Mar 2015)</div>
</div>
</td>
我正在尝试更改 #timeRangeExt
的 CSS 属性,因此我有以下 jQuery:
$("#timeRangeExt").html("TESTING");
但是,它不起作用。我真的很不知所措。我以前已经这样做过 1000 次了,但这次就是不选择。
最佳答案
需要引用完整的id( #timeRangeExt
):
$('#timeRangeExt').css('color','#f0f');
罢工><罢工>罢工>
看来这是一个错字。如果您有两个具有相同值的 ID(如果 qDescription
是重复元素),则您将不得不使用类名。 HTML不能在单个文档上有两个同名的 ID。例如,将其切换为:
<td class="qDescription">
<div>
<div class="questionTitle">% of users who logged in per day</div>
<div class="timeRangeExt"> 05 Mar 2015 - 11 Mar 2015</div>
<div class="compareToLabel"> (26 Feb 2015 - 04 Mar 2015)</div>
</div>
</td>
然后可以引用.qDescription
进行选择:
$('.qDescription .timeRangeExt').css('color','#f0f');
另一个选项是,如果它是动态布局的,则添加一个递增值(例如 el.id = 'timeRangeExt' + increment;
),然后当然可以通过其新 ID( $('#timeRangeExt2').css(...)
)引用它。
关于javascript - jQuery 选择器未选择对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28996855/