开发者

Combining 2 variables in to 1 to call an ID

I am trying to create the following code in a function that will repeat it a variable number of times

myId##.className = myId##.className.replace(' class1',' class2')

so far I have come up with the following but it is not working.

function toggle(partId,orig,rep,itr)
{
var partId; // Begining of Id string
var orig;  // Original Class
var rep;  // Replacement Class
var itr;  // Total number to be iterated through
var num = 1; // Starting at 1
var finalId; // for partId + num
while (num<=itr)开发者_StackOverflow中文版
 {
 finalId = partId + num;
 finalId.className = finalId.className.replace(orig,rep);
 num++;
 }
}

I then implemented it like so.

<area shape="circle" coords="933,92,23" 
      alt="myAlt" href="#none" 
      onmouseover="toggle('myPartId',' class1',' class2',5)"
      onmouseout="toggle('myPartId',' class1',' class2',5)" />

So I thought I could supplement the ID node in className and I am ether doing it wrong or I am wrong in that it can not be done.

Any help would be greatly appreciated.


change your while loop with this

var el;
while (num<=itr)
 {
 finalId = partId + num;
 el = document.getElementById(finalId);
 el.className = el.className.replace(orig,rep);
 num++;
 }
}

and call the chang method and not the toggle.. (unless it is a fault of the example here..)


You never get the object the ID points to. Add an getElementById and try it.

 {
 finalId = partId + num;
 var element = document.getElementById(finalId);
 element.className = element.className.replace(orig,rep);
 num++;
 }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜