好吧Javascript非常新.尝试通过使用外部javascript文件更改按钮上的文本来学习代码.但我甚至无法通过javascript来读取外部的按钮值,在Chrome的调试工具中我看到我的按钮值是btn ="".它读取按钮对象但无法读取其属性.
<html>
<head>
<title> Test </title>
<script type="text/javascript" src="Gle.js"></script>
</head>
<body>
<div><canvas id="Gle" width="800" height="600"></canvas>
</div>
<div>
<h2>Enter the mass and coordinates</h2>
<input id="txtbox" type="text" /><br/>
<button id="btn" onclick="change()">Add</button>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Gle.js
"use strict";
function change() {
var x = document.getElementById("btn").value;
var elem = document.getElementById("btn");
var txt = document.getElementById("txtbox");
txt.text = elem.value;
elem.value = "Ok";
}
Run Code Online (Sandbox Code Playgroud)
当我调试x值时它是"",我的屏幕上没有任何变化.我使用括号IDE.
x是空的,因为'#btn'没有指定'value'属性."添加"字符串位于其内部HTML(或文本)中,
alert(document.getElementById("btn").innerText);
Run Code Online (Sandbox Code Playgroud)
并且您可以this在事件范围中编制索引,它是'#btn',this.innerText.的参考.
另一种方法是获取'#btn'子节点值,即跨浏览器.
alert(this.childNodes[0].nodeValue);
Run Code Online (Sandbox Code Playgroud)
这会警告元素内部指定的第一个文本.