使用JavaScript更改HTML名称属性 [英] Change HTML name attribute using JavaScript

查看:80
本文介绍了使用JavaScript更改HTML名称属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下 div (我将其与其他代码一起制成 button )时,我想要以下代码:

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";
}

当我 alert id的 name 属性时,它表示未定义.怎么办?

When I alert the id's name attribute, 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天全站免登陆