我想動態更改TD標籤的Id。我試過如下動態更改TD標籤的ID不是永久的
var div_node;
div_node = document.getElementById(old_id);
div_node.id = new_id;
但是改變ID的效果並不是永久性的。如果在此之後我寫了一些語句,它只給出原始ID。我的意思是如果我確實提醒(div_node.id),它會給出原始的Id Only。
這是我的整個計劃。
jsAnim.js是JavaScript庫。我已包括它。可在 http://jsanim.com/
<html>
<head>
<script type="text/javascript" src="jsAnim.js"> </script>
<script>
function Assign_id() //This function Will execute onload
{
j=0;
for(i=1;i<4;i++)C
{
document.getElementsByTagName("td")[j].setAttribute("id",i);
//alert(document.getElementsByTagName("td")[j].id);
j++;
}
}
function getdata()
{
var old_id=prompt("Enter Box Id.");
var new_id=prompt("Enter New Position");
animate(old_id,new_id);
change_id(old_id,new_id);
}
function animate(old_id,new_id)
{
var manager=new jsAnimManager();
obj=document.getElementById(old_id);
obj.style.position="relative";
var anim=manager.createAnimObject(old_id);
anim.add({property: Prop.top, to: 100, duration: 2000});
anim.add({property: Prop.left, to: ((new_id-old_id)*50)+4, duration: 2000});
anim.add({property: Prop.top, to: 0, duration: 2000});
obj=document.getElementById(new_id);
obj.style.position="relative";
var anim=manager.createAnimObject(new_id);
anim.add({property: Prop.bottom, to: 100, duration: 2000});
anim.add({property: Prop.right, to: ((new_id-old_id)*50)+4, duration: 2000});
anim.add({property: Prop.bottom, to: 0, duration: 2000});
}
function change_id(old_id,new_id)
{
var div_node;
div_node = document.getElementById(old_id);
div_node.id = new_id;
alert(div_node.id); //HERE IT IS GIVING CORRECT CHANGED ID;
var div_node1 = document.getElementById(new_id);
div_node1.id = old_id;
alert(div_node.id); //BUT HERE IT AGAIN GIVE ORIGINAL ID ONLY
alert(div_node1.id);
}
</script>
</head>
<body onload="Assign_id()">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="50" height="100" background="pic.png">
<font size="10" align="center">A</font>
</td>
<td width="50" height="100" background="pic.png">
<font size="10" align="center">B</font>
</td>
<td width="50" height="100" background="pic.png">
<font size="10" align="center">C</font>
</td>
</tr>
</table>
<button onclick="getdata()"> NEXT </button>
</body>
</html>
什麼是`jsAnimManager`? – Neal 2011-12-15 14:16:14
什麼是`jsAnim.js`,不能只留下代碼,可能是一個問題,還有你交換ID的原因是什麼?爲什麼不去上課呢? (樣式/位置?) – Jakub 2011-12-15 14:21:29