2013-11-01 63 views
0

這裏是我的html:的Javascript選擇合適的元素

<div id="TAB_CLIENTPANE" class="panel_tabs k-widget k-header k-tabstrip" style="height: 328px;" data-role="tabstrip" tabindex="0" role="tablist" aria-activedescendant="TAB_CLIENTPANE_ts_active"> 
    <ul class="k-tabstrip-items k-reset"> 
     <li class="k-item k-state-default k-tab-on-top k-state-active k-first" role="tab" aria-controls="TAB_CLIENTPANE-1" aria-selected="true"> 
      <span class="k-link">Summary Report</span> 
      <a class="tab_win k-button"><span class="k-icon k-i-maximize"></span></a> 
      <a class="tab_close k-button"><span class="k-icon k-i-close"></span></a> 
     </li> 
     <li class="k-item k-state-default k-last" role="tab" aria-controls="TAB_CLIENTPANE-2"> 
      <span class="k-link">Profit Report</span> 
      <a class="tab_win k-button"><span class="k-icon k-i-maximize"></span></a> 
      <a class="tab_close k-button"><span class="k-icon k-i-close"></span></a> 
     </li> 
    </ul> 
    <div class="k-content k-state-active" style="display: block; height: 290px;" aria-expanded="true" role="tabpanel" id="TAB_CLIENTPANE-1"> 
     <div id="POSITIONREPORT" data-role="grid" class="k-grid k-widget k-secondary" style="height: 288px;"> 
      <!--lots of divs etc--> 
     </div> 
    </div> 
    <div class="k-content" style="display: none; height: 290px;" aria-hidden="true" aria-expanded="false" role="tabpanel" id="TAB_CLIENTPANE-2"> 
     <div id="PROFITREPORT" data-role="grid" class="k-grid k-widget k-secondary" style="height: 288px;"> 
      <!--lots of divs etc--> 
     </div> 
    </div> 
</div> 

我想寫的作品如下面的描述中說明的方法:

function get_li(id_of_div){ 
    // the parameter id_of_div will be the id of divs, 
    // eg: "POSTIONREPORT", "PROFITREPORT" etc. 
    // let's say the parameter passed to the function is "PROFITREPORT" 
    // PROFITREPORT is the second div after the ul element, so 
    // the function has to return the second li element of the ul 
} 

我如何有選擇權立?

由於提前,

+0

你能編輯HTML嗎?我的意思是你可以給你的'LI'元素分配一些'ID'或者額外的類嗎? –

回答

1

這將返回正確的li

function get_li(id_of_div){ 
    var index = $('#TAB_CLIENTPANE > div').index($('#' + id_of_div).parent()); 
    return $('#TAB_CLIENTPANE > ul > li').eq(index); 
} 

的jsfiddle示例 - http://jsfiddle.net/zBkyJ/

0
return $('li').eq($('.k-content').index($('#'+id_of_div).parent())); 

這應該得到你所需要的元素

+0

但這段代碼只是整個html的一部分。我如何確保那些是PROFITREPORT的母公司?我指的是TAB_CLIENTPANE。 – anilca