Word leftshift in table cell using Jquery?
<table>
<tr>
<td> word1 word2 word3 word4 </td>
</tr>
</table>
In this above table cell how do I perform left-shift operation on word2
?开发者_如何学编程
- For 1 leftshift it should be like:
word1 word3 word2 word4
- 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)));
精彩评论