如何使用JavaScript更改HTML元素的ID? [英] How do I change the ID of a HTML element with JavaScript?
问题描述
我使用JavaScript修改HTML div
元素客户端的ID。以下代码在Internet Explorer中正常工作,但在Firefox / 2.0.0.20中不工作。它在更新版本的Firefox中有效。
document.getElementById('one')。id ='two'
任何人都可以告诉我:
- 为什么这在FireFox中不起作用。
- Why this doesn't work in FireFox.
- How to make this work in FireFox.
要澄清,我将更改元素ID以引用外部样式表中的不同样式。
它在Firefox中起作用(包括 2.0.0.20
)。请参见 http://jsbin.com/akili (添加 / edit $ c) $ c>到要编辑的网址):
< p id =one>一个< / p&
< a href =#onclick =document.getElementById('one')。id ='two'; return false> Link2< / a>
第一次单击将 id
更改为two
,第二次点击错误,因为现在找不到 id =one
/ p>
也许你有另一个元素已经与 id =two
href =http://www.w3.org/TR/html401/struct/global.html#h-7.5.2 =nofollow noreferrer>您不能有多个元素具有相同的 id
)
I am modifying the ID of an HTML div
element client side with JavaScript. The following code works OK in Internet Explorer but not in Firefox/2.0.0.20. It does work in more recent versions of Firefox.
document.getElementById('one').id = 'two';
Can anyone tell me:
To clarify, I'm changing the element ID to reference a different style in an external style sheet. The style is applied in IE but not in FF.
It does work in Firefox (including 2.0.0.20
). See http://jsbin.com/akili (add /edit
to the url to edit):
<p id="one">One</p>
<a href="#" onclick="document.getElementById('one').id = 'two'; return false">Link2</a>
The first click changes the id
to "two"
, the second click errors because the element with id="one"
now can't be found!
Perhaps you have another element already with id="two"
(FYI you can't have more than one element with the same id
).
这篇关于如何使用JavaScript更改HTML元素的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!