2015-04-18 23 views
-1

我很努力去理解我能如何實現這一點。克隆鏈接,包含特定的文本,並附加到新的李類,只有當寫在HTML中

在HTML

<td class="welcome" rowspan="3"> 
    <b> 
     27412 Commissioner 
    </b> 
    (
    <a href="http://football34.myfantasyleague.com/2015/logout?L=27412"> 
     Logout 
    </a> 
    | 
    <a href="http://football34.myfantasyleague.com/2015/logout?L=27412&BECOME=0001"> 
     Become Owner 
    </a> 
    ) 
    <br></br> 
    <small> 
     <a href="http://football34.myfantasyleague.com/2015/support?L=27412&PROGRAM=commissioner_setup" target="_blank"> 
      Help Center 
     </a> 
    </small> 
</td> 

我需要有一個腳本,如果td.welcome有文字「成爲所有者」或「成爲委員」,克隆

目前並追加特定A HREF鏈接和文本

然後在HTML某處esle我想添加一個新的名爲黎類

<li class="become"></li> 

,並有鏈接和文字顯示appened它,所以生成的HTML看起來如T他

<li class="become"> 
     <a href="http://football34.myfantasyleague.com/2015/logout?L=27412&BECOME=0001"> 
      Become Owner 
     </a> 
</li> 
+0

請向我們提供您所需的HTML輸出的採樣。 – Cristik

+0

你在JS或JQuery上做了什麼?你如何解析你的代碼? – aorfevre

+0

已更新,以顯示所需的HTML,但希望將一個類和文本添加到li.become,所以我可以簡單地放置該li類並將鏈接顯示在裏面。我根本不知道jquery – MShack

回答

2

//Find nodes that contain "Become Owner" or "Become Commissioner" in nodes with the class "welcome" 
 
var $link = $('.welcome :contains("Become Owner"), .welcome :contains("Become Commissioner")'); 
 

 
//If we have results 
 
if($link.length > 0){ 
 
    //append a clone of the first result to the nodes with the class "become" 
 
    $('.become').append($link[0].cloneNode(true)); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
     <tr> 
 
     <td class="welcome" rowspan="3"> 
 
      <b> 
 
       27412 Commissioner 
 
      </b> 
 
      (
 
      <a href="http://football34.myfantasyleague.com/2015/logout?L=27412"> 
 
       Logout 
 
      </a> 
 
      | 
 
      <a href="http://football34.myfantasyleague.com/2015/logout?L=27412&BECOME=0001"> 
 
       Become Owner 
 
      </a> 
 
      ) 
 
      <br></br> 
 
      <small> 
 
       <a href="http://football34.myfantasyleague.com/2015/support?L=27412&PROGRAM=commissioner_setup" target="_blank"> 
 
        Help Center 
 
       </a> 
 
      </small> 
 
     </td> 
 
    </tr> 
 
</table> 
 
<ul> 
 
    <li class="become"></li> 
 
</ul>

+0

Ty Brandon,was搜索周圍,發現這也適用,這是更好的使用,你或下面$(「li.become」)。append($(「td.welcome a:contains('Owner)')); $(「li.become」)。append($(「td.welcome a:contains('Become Commissioner')」)); – MShack

+0

這些選擇器更具體一些,因爲除了'class'和':contains'選擇器外,它們僅限於'li','td'和'a'節點。如果您希望類別成爲「受歡迎」以存在於不同類型的節點上,它可能會更可靠。而且,這段代碼不會「克隆」它找到的節點,它實際上將它從原始位置移除並放置在「li.become」節點中。所以你仍然需要調用'cloneNode',這樣鏈接就會顯示在兩個地方。 –

+0

完美,這就是我想要的。再次感謝 ! – MShack

相關問題