javascript - 被 DOM 树中的子对象覆盖的 JS dom 对象的属性?

标签 javascript html dom properties

在 HTML 代码中,我有一个表单标签:

<form id='my_form' action='url.hmtl'> ... </form>

对应的JS对象有一个名为'action'的属性,其中包含字符串'url.html':

my_form=document.getElementById('my_form');
console.log(form.action);

以上代码显示:url.hmtl 但是如果我在表单中添加一个子对象,比如一个输入,id='action':

<form id='my_form' action='url.hmtl'>
  <input type="hidden" id="action" />
</form>

那么form.action现在就是输入对应的JS对象了。

我的问题:

  1. 这种行为正常吗?
  2. 我应该禁止自己使用字符串“action”作为任何输入的 ID 吗?
  3. 如果有这样的输入,是否有任何方法可以获取表单的原始属性“Action”,或者它会永远丢失吗?

最佳答案

Is this behaviour normal?

Yes, unfortunately .

Should I forbid myself to use the string 'action' as id of any input?

是的,那个and many more .

If there is such an input, is there any way to get the original property 'Action' of the form or is it lost for ever?

该属性仍在 DOM 中,当您删除输入时,action 属性将再次代表该属性的值。在那之前,您可以使用 getAttribute method表单元素。

关于javascript - 被 DOM 树中的子对象覆盖的 JS dom 对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25548303/

相关文章:

javascript - 进行两个系列的异步调用,中间有延迟

javascript - Truffle JS 测试不起作用

JavaScript - 从文件夹扫描并创建链接文件

javascript - 在模态弹出窗口内插入图像

javascript - 如何使用 JavaScript 查找设置为 'auto' 的元素的实际渲染值

html - IE8 为非显示元素保留空间

javascript - 如何确保提交表单时不显示另一个(新)页面

javascript - 使用 jquery 动画几个进度条

javascript - 获取当前环境可用的事件类型

Javascript 在同一张图片上单击播放和暂停歌曲