使用JavaScript更改HTML名称元素
问题描述:
按div(我将它与其他代码制成一个按钮)时,我想要以下代码:
On the press of a div (which I made into a button with other code) I would like the following code:
<div id="w1" name='w1-0' onclick="weekclick(id)">1<br /></div>
...用于将名称更改为 name ='w1-0 '
到 name ='w1-1'
...for the name to change from name='w1-0'
to name='w1-1'
我正在使用以下内容JavaScript:
I'm using the following JavaScript:
function weekclick(id) {
document.getElementById(id).input.name = "w1-1";
}
当我提醒ID名称时,它说是未定义的。做什么?
When I alert the id's name, it says it is undefined. What do?
答
请参见下面的代码:
<div id="w1" name='w1-0' onclick="weekclick(id)">1<br /></div>
<div onclick="weekclick('w1');">click me</div>
<script type="text/javascript">
function weekclick(id) {
document.getElementById(id).setAttribute("name","w1-1");
}
</script>
链接到小提琴- http://jsfiddle.net/TH9C2/
(从小提琴中删除警报行-在那里只是向您显示起作用)
(remove the alert line from the fiddle - it is just there to show you that it works)