在标签中包含li的内容

时间:2012-09-14 09:30:01

标签: jquery forms hyperlink html-lists

我正在使用此插件:http://www.jquerynewsticker.com/ 我需要将li的内容包含在<a href=""></a>

<ul id="js-news">
    <li class="news-item">This is text 1 ...</li>
    <li class="news-item">This is text 2 ...</li>
    <li class="news-item">This is text 3 ...</li>
    <li class="news-item">This is text 4 ...</li>
 </ul>
像这样:

<ul id="js-news">
        <li class="news-item"><a href="my-form">This is text 1 ...</a></li>
          <li class="news-item"><a href="my-form">This is text 2 ...</a></li>
          <li class="news-item"><a href="my-form">This is text 3 ...</a></li>
          <li class="news-item"><a href="my-form">This is text 4 ...</a></li>
</ul>

我认为字符串需要在这个中。但我不知道如何:

script type="text/javascript">
    $(function () {
        $('#js-news').ticker({



        });
    });
</script>

2 个答案:

答案 0 :(得分:4)

试试这个:

$('li.news-item').each(function(){
  $(this).contents().wrap('<a href="#"/>');
});

演示: http://jsbin.com/uqedeq/1/edit

答案 1 :(得分:1)

我建议,只要您的用户不会/不能插入自己的链接元素:

$('li.news-items').contents().wrap('<a href="#" />');​

JS Fiddle demo