开发者

Javascript: Append string in specific offset position of an input

Is there a way to append a string in a specific offset position on an input? Suppose we have the following input : an offset : <input type="text" value="abcdef"/>, the letter c is situated in the 3th offset of the input string value.

+-------------+

123456789

+-------------+

xxxxxxx->offset position

L开发者_如何学运维et's say I want to append the string "hi" in the 3th poistion of the input so it become :

+-------------+

   hi

+-------------+

123456789

How can I do that?

thanks.


You could very easily write your own function to do that.

function insertAt(src, index, str) {
    return src.substr(0, index) + str + src.substr(index)
}
alert(insertAt("test", 2, "FOO")); //Alerts teFOOst
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜