简体   繁体   English

jQuery选择器未选择对象

[英]jQuery selector not selecting object

So I have some HTML. 所以我有一些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>

I'm trying to change the CSS property of #timeRangeExt , so I have the following jQuery: 我正在尝试更改#timeRangeExt的CSS属性,因此具有以下jQuery:

$("#timeRangeExt").html("TESTING");

However, it doesn't work. 但是,它不起作用。 I'm really at a loss here. 我真的很茫然。 I've done this 1000 times before, but this time it just won't select. 我已经做过1000次了,但是这次只是选择了。

Need to reference the complete id ( #timeRangeExt ): 需要引用完整的ID( #timeRangeExt ):

 
 
 
  
  $('#timeRangeExt').css('color','#f0f');
 
  

It appears it was a typo. 看来是错字。 If you have two IDs with the same value (if qDescription is a repeated element) you're going to have to use a class name instead. 如果您有两个具有相同值的ID(如果qDescription是重复的元素), qDescription不得不使用类名。 HTML cannot have two IDs with the same name on a single document. HTML在一个文档中不能有两个具有相同名称的ID。 For example, switch it up to: 例如,将其切换为:

<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>

Then you can select it in reference to .qDescription : 然后,您可以参考.qDescription选择它:

$('.qDescription .timeRangeExt').css('color','#f0f');

Another options is, if it's dynamically laid out, is to add an incrementing value (eg el.id = 'timeRangeExt' + increment; ) then, of course, reference it by its new id ( $('#timeRangeExt2').css(...) ). 另一个选择是,如果它是动态布局的,则是添加一个递增值(例如el.id = 'timeRangeExt' + increment; ),然后当然$('#timeRangeExt2').css(...)其新的id( $('#timeRangeExt2').css(...)引用它$('#timeRangeExt2').css(...) )。

您的代码中有错字,应该像

$("#timeRangeExt").html("TESTING");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM