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 に答える