开发者

Word leftshift in table cell using Jquery?

开发者 https://www.devze.com 2023-03-18 03:23 出处:网络
<table> <tr> <td> word1 word2 word3 word4 </td> </tr> </table> In this above table cell how do I perform left-shift operation on word2?开发者_如何学编程
<table>
  <tr>
     <td> word1 word2 word3 word4 </td>
  </tr>
</table>

In this above table cell how do I perform left-shift operation on word2?开发者_如何学编程

  1. For 1 leftshift it should be like: word1 word3 word2 word4
  2. For 2 leftshifts it should be like: word1 word3 word4 word2


So assuming your table-cell has the id foo:

function shiftSecondWord(inner, count) {
    var words = inner.split(" ");
    // Remove second word
    var secondWord = words.splice(1, 1)[0];
    // insert word again offset by count
    words.splice(count + 1, 0, secondWord);
    return words.join(" ");
}

To shift the content of #foo 2 to the right you would:

$("#foo").text(shiftSecondWord($("#foo").text(), 2)));
0

精彩评论

暂无评论...
验证码 换一张
取 消