js可以改变dom元素的id并保存吗
喜氏天狼 2014-07-07 02:34:21 这是一个js函数中的一句:table1+='<td>'+weihao+'</td><td>'+No+'</td><td><a href="#" onclick="editId('+what+')">修改'+card+'id</a></td>';
这是它调用的那个函数:function editId(which){
alert("原来的id是:"+which.getAttribute("id"));
var newId=prompt("请输入新的id");
which.setAttribute(0,newId);
}
我试了之后发现这个函数根本不执行。不知道哪里出问题了。
还有就是我用html试了一下,这种方法是可以改变元素的id的,代码为:
<style type="text/css">
#aaa{ width:200px; height:400px; background-color:#0F6;}
#bbb{ width:500px; height:200px; background-color:#F00;}
</style>
<script type="text/javascript">
function editId(which){
which.setAttribute("id","bbb");
}
</script>
</head>
<body>
<div id="aaa" onclick="editId(this)"></div>
</body>
但是这种方法改变id我只是通过它颜色的改变知道的,但是我查看源代码的话还是原来的id。不知道有没有方法可以让我们把改变后的id保存下来吗?