Stmixing string with array variable to assign an id for an element in javascript

I´m trying to mix a string to assign variable(array) I don´t know what´s wrong I can not get it work. in **php** I will send array of id to javascript via json_encode(); I will get like :**key**= 1, 2, 3 etc. Here is aline of those div:
text
Then in **javascript** with a conditon like this: $("#swas"+key).removeClass('colorme'); function xx(key, arr) { for (var v in arr) { //LOOP var k = arr[v]; if ($("#swas" + k) != key) { $("#swas" + k).addClass('colorme'); } } } What have I done wrong ? **UPDATE** The reason I want to mix "swas" with array because in php page there´re alot of div that name swas1, swas2, swas3, swas4.........>etc And "key" is the id of current div that will be clicked. and "key" value : 001, 002, 003, 004 etc What I want to do is to make the other div (that´s not the current div )to not change color. that´s why I have to mix the word "swas" with the "key" in javascript. **UPDATE2** Now It work with script above but new probelm, it not remove the class when clicked :S **UPDATE3** Now everything just work fine after I move the $("#swas"+key).removeClass('colorme'); To the bottom :) S

以上就是Stmixing string with array variable to assign an id for an element in javascript的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏