最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Coffeescript: I can't seem to select an element by ID + child class - Stack Overflow

programmeradmin2浏览0评论

I'm trying to select a div where class= pick1-box given only the ID of a parent using Coffeescript.

jQuery(document).ready ->

  pick1value = $('#vote_pick1_id').val
  $("#" + pick1value + " .pick1-box").css('background-color', 'green')

I can verify that pick1value has received a value from $('#vote_pick1_id').val

...
<li class='nominee clearfix' id='146'>
  <div class='candidate'>
    <img alt="Enders" height="80" src="/assets/25803sm.jpg" />
    Dick Waddington
  </div>
  <div class='pick-boxes'>
    <div class='pick1-box'>
      1
    </div>
    <div class='pick2-box'>
      2
    </div>
  </div>
</li>
...

FWIW: $("#" + pick1value) doesn't seem to work either.

I'm trying to select a div where class= pick1-box given only the ID of a parent using Coffeescript.

jQuery(document).ready ->

  pick1value = $('#vote_pick1_id').val
  $("#" + pick1value + " .pick1-box").css('background-color', 'green')

I can verify that pick1value has received a value from $('#vote_pick1_id').val

...
<li class='nominee clearfix' id='146'>
  <div class='candidate'>
    <img alt="Enders" height="80" src="/assets/25803sm.jpg" />
    Dick Waddington
  </div>
  <div class='pick-boxes'>
    <div class='pick1-box'>
      1
    </div>
    <div class='pick2-box'>
      2
    </div>
  </div>
</li>
...

FWIW: $("#" + pick1value) doesn't seem to work either.

Share Improve this question asked Feb 23, 2012 at 16:56 MeltemiMeltemi 38.4k52 gold badges201 silver badges298 bronze badges 3
  • 1 fyi jQuery(document).ready -> can be written $ -> – asawyer Commented Feb 23, 2012 at 16:58
  • ID's cannot start with numbers, I would start by fixing that. – Kevin B Commented Feb 23, 2012 at 17:01
  • What value does pick1value contain? I don't see anything with an id attribute that also has a pick1-box class. – SenorAmor Commented Feb 23, 2012 at 17:02
Add a ment  | 

1 Answer 1

Reset to default 3

This...

pick1value = $('#vote_pick1_id').val

should be this...

pick1value = $('#vote_pick1_id').val()

because you're not passing arguments.

Right now you're assigning the function itself to the variable instead of calling it.


Pretty sure your .css() call could eliminate the () though...

$("#" + pick1value + " .pick1-box").css 'background-color', 'green' 
发布评论

评论列表(0)

  1. 暂无评论