带原型的第N个子选择器

时间:2013-12-03 17:10:24

标签: javascript jquery ajax prototypejs

我正在尝试使用Prototype中的“nth-child(n)”,就像我使用jQuery一样。代码示例如下......

function myFunction()
{
$$('div.amfinder-horizontal td:nth-child(1) select').simulate('click');
$$('div.amfinder-horizontal td:nth-child(1) select').simulate('change');
}

Event.observe(window,"load",myFunction);

不幸的是,这似乎没有选择元素(因为点击和更改不起作用)。当我把select的类放在那里时,它确实有用......但我不能使用这个类,需要通过nth-child来完成它。我相信:第一个和最后一个有效,但我总共需要6个才能做到这一点......即使这样,它也不会总是全部6个。

如果有人可以提供nth-child的例子以及它如何与Prototype一起使用,那就太棒了!

<div class="amfinder-horizontal" id="amfinder_529e1406aff23Container">
   <table>
      <tbody>
         <tr>
            <td>
               <div class="dropdown-title">Year</div>
               <div class="dropdown-element active">
                  <select name="finder[424]" id="finder-72--424" style="width: 101px;" class="active">
                     <option value="0">
                        Year                        
                     </option>
                     <option value="8736830">
                        2013                        
                     </option>
                     <option value="8734482">
                        2012                        
                     </option>
                     <option value="8734488">
                        2011                        
                     </option>
                     <option value="8734487">
                        2010                        
                     </option>
                     <option value="8734481">
                        2009                        
                     </option>
                     <option value="8734486">
                        2008                        
                     </option>
                     <option value="8734485">
                        2007                        
                     </option>
                     <option value="8734484">
                        2006                        
                     </option>
                     <option value="8734494">
                        2005                        
                     </option>
                     <option value="8734483">
                        2004                        
                     </option>
                     <option value="8734489">
                        2003                        
                     </option>
                     <option value="8734492">
                        2002                        
                     </option>
                     <option value="8734491">
                        2001                        
                     </option>
                     <option value="8734490">
                        2000                        
                     </option>
                     <option value="8734493">
                        1999                        
                     </option>
                     <option value="8734496">
                        1998                        
                     </option>
                     <option value="8734495">
                        1997                        
                     </option>
                     <option value="8734501">
                        1996                        
                     </option>
                     <option value="8734500">
                        1995                        
                     </option>
                     <option value="8734499">
                        1994                        
                     </option>
                     <option value="8734498">
                        1993                        
                     </option>
                     <option value="8734497">
                        1992                        
                     </option>
                     <option value="8734503">
                        1991                        
                     </option>
                     <option value="8734502">
                        1990                        
                     </option>
                     <option value="8734504">
                        1989                        
                     </option>
                     <option value="8734505">
                        1988                        
                     </option>
                     <option value="8734508">
                        1987                        
                     </option>
                     <option value="8734507">
                        1986                        
                     </option>
                     <option value="8734506">
                        1985                        
                     </option>
                     <option value="0">Year</option>
                  </select>
                  &nbsp;
                  <span class="arrow"></span>
               </div>
            </td>
            <td>
               <div class="dropdown-title">Make</div>
               <div class="dropdown-element">
                  <select name="finder[425]" id="finder-72--425" disabled="disabled" style="width:120px">
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                     <option value="0">Make</option>
                  </select>
                  &nbsp;
                  <span class="arrow"></span>
               </div>
            </td>
            <td>
               <div class="dropdown-title">Model</div>
               <div class="dropdown-element">
                  <select name="finder[426]" id="finder-72--426" disabled="disabled" style="width:120px">
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                     <option value="0">Model</option>
                  </select>
                  &nbsp;
                  <span class="arrow"></span>
               </div>
            </td>
            <td>
               <div class="dropdown-title">Sub Model</div>
               <div class="dropdown-element">
                  <select name="finder[427]" id="finder-72--427" disabled="disabled" style="width:120px">
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                     <option value="0">Sub Model</option>
                  </select>
                  &nbsp;
                  <span class="arrow"></span>
               </div>
            </td>
            <td>
               <div class="dropdown-title">Engine</div>
               <div class="dropdown-element">
                  <select name="finder[428]" id="finder-72--428" disabled="disabled" style="width:120px">
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                     <option value="0">Engine</option>
                  </select>
                  &nbsp;
                  <span class="arrow"></span>
               </div>
            </td>
            <td>
               <div class="dropdown-title">Position</div>
               <div class="dropdown-element">
                  <select name="finder[429]" id="finder-72--429" disabled="disabled" style="width:120px">
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                     <option value="0">Position</option>
                  </select>
                  &nbsp;
                  <span class="arrow"></span>
               </div>
            </td>
         </tr>
         <tr>
            <td colspan="6">
               <br>
               <div class="amfinder-buttons" style="display:none">
                  <button id="findbtn" class="btn" title="Find" type="submit"><span><span>Find</span></span></button>
                  <button class="btn" name="reset" value="1" title="Reset" type="submit"><span><span>Reset</span></span></button>
               </div>
            </td>
         </tr>
      </tbody>
   </table>
</div>

3 个答案:

答案 0 :(得分:0)

您已使用过$次。

可能是造成问题的原因。

更改

$$('div.amfinder-horizontal td:nth-child(1) select').simulate('click');
$$('div.amfinder-horizontal td:nth-child(1) select').simulate('change');

$('div.amfinder-horizontal td:nth-child(1) select').simulate('click');
$('div.amfinder-horizontal td:nth-child(1) select').simulate('change');

答案 1 :(得分:0)

您可以像数组样式一样选择它们

http://jsbin.com/eDAWaji/1/edit

HTML:

<ol class="myclass">
  <li>lista</li>
  <li>lista</li>
  <li>lista</li>
  <li>lista</li>
  <li>lista</li>      
</ol>

JS:

var li = $$('body > ol.myclass li');
li[3].style.background = 'red';

答案 2 :(得分:0)

$$()是CSS选择器方法,它返回一个元素数组

$$('div.amfinder-horizontal td:nth-child(1) select').simulate('click');

将无效,因为您无法在阵列上运行模拟

只要你加载了Event.simulate

,这应该可行
$$('div.amfinder-horizontal td:nth-child(1) select').invoke('simulate','click');