更新1
在得到一些答案后,我想出了一个更复杂的案例。
当表单具有名为"ID"one_answers"getAttribute"的字段时,如何访问表单的ID和其他属性?一般的问题是:如何在任何情况下可靠地访问表单的属性?
console.log(document.querySelector("#myform").id)
console.log(document.querySelector("#myform").getAttribute("id"))
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Form</title>
<form id="myform">
<input name="id" type="text" value="myvalue">
<input name="getAttribute" type="text" value="myvalue2">
</form>
结果:
<input name="id" type="text" value="myvalue">
TypeError: document.querySelector(...).getAttribute is not a function[Learn More]
我很困惑表单的字段是如何覆盖表单的属性的。例如,一个名为"id"的字段将隐藏表单的实际id:
console.log(document.querySelector("#myform").id)
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Form</title>
<form id="myform">
<input name="id" type="text" value="myvalue">
</form>
给出:
<input name="id" type="text" value="myvalue">
如何访问表单属性而不存在表单字段覆盖它们的危险(以防字段可能有任何名称)?
使用getAttribute()可以获得表单属性。
console.log(document.getElementById('myform').getAttribute('id'))
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Form</title>
<form id="myform">
<input name="id" type="text" value="myvalue">
</form>
这与表单和表单字段的关系不大,更多的是与您为元素提供了name
这一事实有关,CCD_1是一个实际的本地HTML属性和JavaScript属性。结果,你没有得到你想要的推荐信。
您的代码发生的情况是,document.querySelector("#myform")
正确地定位了form
元素,但随后您要求其id
属性,因此,由于还有另一个元素的name
与form
的id
相同,控制台记录元素而不是实际属性值,因为表单字段是其CCD_。
如下所示,只需请求document.querySelector("#myform")
(后面没有.id
),就会返回对与选择器匹配的第一个DOM元素的引用。
console.log(document.querySelector("#myform").id);
console.log(document.querySelector("#myform"));
<form id="myform">
<input name="id" type="text" value="myvalue">
</form>
因此,不要给元素一个name
或id
,这是HTML元素的本机属性或属性的名称,这样就可以了:
console.log("The value of the id attribue of the form as well as the value of the id property of the form DOM object is: " + document.querySelector("#myform").id); // Now returns the id of the element
console.log("The input element within the form is: ", document.querySelector("input[name='user']"));
<form id="myform">
<input name="user" type="text" value="myvalue">
</form>
编辑:
OP现在已经将问题更改为包含getAttribute
或任何任意字符串作为表单元素名称。
这与覆盖任何现有变量没有什么不同。例如,如果我想创建一个名为window
或document
的变量,该怎么办?或者我想将一些数据存储在名为forms
的变量中,并将其放置在现有的document
对象中。
每一项都会奏效,但不会产生预期的结果。这就是这个问题的本质。"我能做这个吗?"是的。"我应该这样做吗?"不,
作为开发人员,我们有义务理解这些细微差别。现在OP做到了。
最令人沮丧的是,OP现在知道了这个真相,但却继续通过更改被覆盖的变量来追求这个问题!
结束编辑(关于使用id
作为名称的原始答案):
您混淆了Object属性和HTML属性之间的区别。
我们使用对象访问方法(.
运算符或[]
表示法)访问属性。
我们使用.getAttribute('name')
访问HTML属性。因此,如果你想要像你的问题所建议的那样,用HTML编写属性的值,你可以使用:
document.querySelector("#myform").getAttribute('id')
,返回"myform"。
但是,就像大多数思考"如果"而没有真正的问题需要解决的问题一样,这似乎有点傻——毕竟,元素是被查询使用其id的。
现在,以正确的方式考虑对象属性,这是:
document.querySelector("#myform").id
正如规范所说的那样评估语法:
表单[名称]
返回的窗体控件(或者,如果有多个,则返回的RadioNodeList表单控件)由于历史原因的图像按钮);或者,如果没有,返回具有给定ID的img元素。
使用特定名称引用元素后,该名称将继续作为引用中该元素的一种方式该方法,即使元素的实际ID或名称发生更改,例如只要元素保留在树中。
如果有多个匹配项,则RadioNodeList对象返回包含所有那些元素的。
此处:https://html.spec.whatwg.org/multipage/forms.html#the-表单元素
希望能有所帮助。
我提出了两个相当大的构造,它们必须工作,而不考虑任何可能的字段名。这个想法是借用干净对象中的方法和原型中的访问器。
必须从原型链中选择一个正确的原型(你必须知道属性是如何定义的)。
// To access obscured methods (and also attributes)
console.log(document.createElement("form").getAttribute.apply(document.querySelector("#myform"), ["id"]))
// To access obscured properties (such as `style`):
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Object.getPrototypeOf(document.createElement("form"))), "style").get.apply(document.querySelector("#myform")).background = "green"
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Form</title>
<form id="myform">
<input name="id" type="text" value="myvalue">
<input name="getAttribute" type="text" value="myvalue2">
<input name="style" type="text" value="myvalue3">
</form>