在 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对象了。
我的问题:
- 这种行为正常吗?
- 我应该禁止自己使用字符串“action”作为任何输入的 ID 吗?
- 如果有这样的输入,是否有任何方法可以获取表单的原始属性“Action”,或者它会永远丢失吗?
最佳答案
Is this behaviour normal?
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/