【问题标题】:Wrap li's content in a tag将 li 的内容包装在标签中
【发布时间】:2012-09-07 10:53:20
【问题描述】:

我正在使用这个插件: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>

【问题讨论】:

  • 为什么用户不能编辑cms中的li?
  • @Kasper 在锚标签内包裹 li 元素在语义上是不正确的。
  • @freebird 我很确定它是有效的 HTML5,但在这种情况下,最好将 a 放在 li
  • @jtheman:最终用户不知道该怎么做。
  • @freebird:不是li,而是li里面的内容

标签: jquery forms hyperlink html-lists


【解决方案1】:

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

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

JS Fiddle demo.

【讨论】:

  • 它在某些时候有效,但我认为我必须加入这个$('#js-news').ticker( ); 因为我正在使用这个插件:jquerynewsticker.com 但我已经尝试了一段时间,但我无法获得它工作..
【解决方案2】:

试试这个:

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

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

【讨论】:

  • 我认为您不需要遍历选择中的元素。
猜你喜欢
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2014-05-03
  • 2013-02-09
  • 1970-01-01
相关资源
最近更新 更多