1

试图找到一种将第一个单词包装在 span 标签中的方法。要换行的文本总是在最后一个链接后面

 <div class="style1">
    <a class="style2" href="http://www.example.com">a</a>
    <a class="style3" href="http://www.example.com">b</a>
    test test test test test test test 
</div>

我希望第一个test被包裹<span>test</span>

它将在此处用于更深入的解决方案

小提琴:http: //jsfiddle.net/evanmoore/awwTA/1/

4

2 回答 2

2
$('.pag').contents().filter(function () {
  // IE doesn't support `textContent` proerty, you can replace it with $(this).text()
  if (this.nodeType === 3 && $.trim(this.textContent) !== '') {
      $(this).wrap('<div/>').parent().html(function (i, v) {
           return v.replace(/(\w+\s)/, '<span>$1</span>')
      }).replaceWith(function() {
           return this.innerHTML;
      })
  }
})

http://jsfiddle.net/XKjtq/

于 2013-01-11T05:42:04.430 回答
0

试试这段代码:

<script>
    data = $(".style1").html().trim().split("\n");
    str = data[data.length - 1];
    data.splice(data.length - 1, 1);
    str = str.trim().split(" ");
    str[0] = "<span>" + str[0] + "</span>";
    data[data.length] = str.join(" ");
    $(".style1").html(data.join("\n"));
</script>
于 2013-01-11T05:50:54.537 回答