使用JavaScript更改HTML名称元素 [英] Change HTML name element using JavaScript

查看:91
本文介绍了使用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)

这篇关于使用JavaScript更改HTML名称元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆