2015-04-18 24 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

相关问题