2015-10-05 68 views
1

最後一個元素,有沒有簡單的方法來從jQuery的下面結構中添加李添加Li從jQuery的

<div id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66" > 

    <ol class="top-menu-bar pull-right"> 
    <li> 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbLanguage" href="/ar/">Test</a> 
    </li> 
    <li> 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbSitemap" href="/en/sitemap">Sitemap</a> 
    </li> 
    <li> 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbContact" href="/en/contact-us?tab=2">Contact</a> 
    </li> 
    <li> 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbFaq" href="/en/contact-us?tab=3">FAQ</a> 
    </li>  
    </ol> 

</div> 

我想回答問題後增加以下李

<li> 
      <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbFaq" href="/en/contact-us?tab=3">FAQ</a> 
      </li> 

我在下面嘗試,但它在所有李時珍

$("#ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66 li").last().html('<li> Menu 5 </li>'); 

回答

3

使用after()以櫻雪addind RT元素後

$("#ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66 li").last().after('<li> Menu 5 </li>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66"> 
 

 
    <ol class="top-menu-bar pull-right"> 
 
    <li> 
 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbLanguage" href="/ar/">Test</a> 
 
    </li> 
 
    <li> 
 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbSitemap" href="/en/sitemap">Sitemap</a> 
 
    </li> 
 
    <li> 
 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbContact" href="/en/contact-us?tab=2">Contact</a> 
 
    </li> 
 
    <li> 
 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbFaq" href="/en/contact-us?tab=3">FAQ</a> 
 
    </li> 
 
    </ol> 
 

 
</div>

,或者您可以使用append()的附加內容

$("#ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66 ol").append('<li> Menu 5 </li>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66"> 
 

 
    <ol class="top-menu-bar pull-right"> 
 
    <li> 
 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbLanguage" href="/ar/">Test</a> 
 
    </li> 
 
    <li> 
 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbSitemap" href="/en/sitemap">Sitemap</a> 
 
    </li> 
 
    <li> 
 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbContact" href="/en/contact-us?tab=2">Contact</a> 
 
    </li> 
 
    <li> 
 
     <a id="ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66_lbFaq" href="/en/contact-us?tab=3">FAQ</a> 
 
    </li> 
 
    </ol> 
 

 
</div>

+0

Thinks ..它的工作原理 – user788592

+0

我會在12分鐘後將它標記爲正確 – user788592

+1

@ user788592:很高興幫助:) –

2

使用jQuery的After()方法:

$("#ctl00_g_d75240ee_258b_420a_b571_2b97dcda8a66 li").last().after('<li> Menu 5 </li>'); 

這是Fiddle

相關問題