开发者

Changing only part of attributes value in JavaScript

I have 3D model, that is represented as an HTML element with COORDS attribute having "x y z ... x y z" coordinates. If model is small (COORDS value is relatively short) then its no problem to split the string (put values into array), change specific values (i know position/index), and join array (get string back together).

So, is there a way to get/update part开发者_如何学运维s of COORDS value, while only knowing position/index in the string?


You could use the substr function to split the string at the position where you want to change it. This way you would only end up with two part string.

Basically something like this:

var str = 'foo bar baz';
var start = str.substr(0, 4); //start is now 'foo '
var end = str.substr(7); //end is now ' baz'

var newStr = start + 'hello' + end; //foo hello baz

For this to work you would need to know the exact index in the string plus the length of the data you wish to replace. It may be easier to just split it as long as there are no performance problems with it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜