javascript - 如果没有 getElement 方法,这个代码的简写是如何工作的,有人可以解释一下并告诉更多关于这个类比的信息
问题描述
我正在翻阅我的书,发现一段代码没有使用传统getElement
的选择方法,但使用了一些速记。有人能告诉我更多关于这件事的信息吗?我对此一无所知frm.txt.value
。
function show_temp(){
var Tval=frm.txt.value;
alert("The temp in far. is " + Tval + " degree");
}
<form name="frm">
Enter the temp in Fahrenheit:
<input type="text" name="txt">
<input type="button" value="temperature" onclick="show_temp()">
</form>
解决方案
您所指的简写与the和 the的name
属性有关-这可用于引用 JavaScript 中的元素,并可用作etc的替代品。form
input
getElementById
function show_temp(){
console.log(frm.txt);
}
<form name="frm">
Enter the temp in Fahrenheit:
<input type="text" name="txt">
<input type="button" value="temperature" onclick="show_temp()">
</form>
但是,不建议这样做 - 与 不同id
,name
属性可以在整个页面中重复,因此frm.txt
在下面的第二个片段中引用实际上会返回一个结果数组而不是单个结果(单击两者上的温度按钮进行比较)。
function show_temp(){
console.log(frm.txt);
}
<form name="frm">
Enter the temp in Fahrenheit:
<input type="text" name="txt">
<input type="text" name="txt">
<input type="button" value="temperature" onclick="show_temp()">
</form>
推荐阅读
- c# - 从 Visual Studio 扩展调试 C# 文件
- php - PHP中的注册系统
- mongodb - 如何为 MongoDB 中的每个唯一值分组并获取具有最大日期的文档
- security - 设置 Pid cgroup 限制
- wcf - 如何通过 WCF SOAP 服务访问 EF 导航属性?
- azure-cognitive-search - 如果句子有特殊字符,Azure 搜索突出显示不起作用
- matlab - Matlab中的break语句
- airflow - 从调度间隔 = @once 的主 dag 触发外部 dag 的最佳方法?
- node.js - 在 Azure Functions (NodeJS) 中处理多个 HTTP 方法的正确方法是什么
- javascript - 如何将大量对象中的某些列/字段相乘?