简单的JavaScript函数没有';I don’我不能毫无差错地工作
我是js新手,我的第一个也是最简单的函数不是没有错误的 其他js代码工作正常,但不是这个 代码如下:我希望它只打印我在输入字段中写的内容 HTML文件:简单的JavaScript函数没有';I don’我不能毫无差错地工作,javascript,Javascript,我是js新手,我的第一个也是最简单的函数不是没有错误的 其他js代码工作正常,但不是这个 代码如下:我希望它只打印我在输入字段中写的内容 HTML文件: <html> <head></head> <body> <input type="text" id="myid"> <button onclick="myfunc()">click</button>
<html>
<head></head>
<body>
<input type="text" id="myid">
<button onclick="myfunc()">click</button>
<script src="javascript.js"></script>
</body>
</html>
您不需要对该值进行任何处理,只需检索该值并将其丢弃。试一试
function myfunc() {
alert(document.getElementById("myid").value);
}
您不需要对该值进行任何处理,只需检索该值并将其丢弃。试一试
function myfunc() {
alert(document.getElementById("myid").value);
}
这是一个简单的例子,这个函数什么都不做 您的代码在获取id为myid的输入字段值方面是正确的 但是在这之后,您就不需要对它做任何操作了,如果您想查看它,您可以对值执行
console.log()
,或者您可以将它分配给一个变量,以便稍后使用它const inputValue=document.getElementById(“myid”).value代码>
行document.getElementById(“myid”).value代码>只是检索值,不做任何处理,只是将其丢弃,这就是为什么您看不到任何错误或反馈。这是一个简单的情况,该函数不做任何事情
您的代码在获取id为myid的输入字段值方面是正确的
但是在这之后,您就不需要对它做任何操作了,如果您想查看它,您可以对值执行console.log()
,或者您可以将它分配给一个变量,以便稍后使用它const inputValue=document.getElementById(“myid”).value代码>
行document.getElementById(“myid”).value代码>只是检索值,不做任何处理,只是将其丢弃,这就是为什么您看不到任何错误或反馈。如果您想打印输入字段的值,可以使用console.log()
将其内容打印到控制台。例如,将输入值保存在变量中。然后,您可以在浏览器的控制台中记录此变量的值,或将其用于其他目的
function myfunc() {
"use strict";
var value = document.getElementById("myid").value;
console.log(value)
}
document.getElementById(“myid”).value
仅获取输入的值。但你什么都没做
您也可以直接记录该值,而无需先将其保存到变量,请参见下面的示例
function myfunc() {
"use strict";
console.log(document.getElementById("myid").value)
}
<html>
<head></head>
<body>
<input type="text" id="myid">
<button onclick="myfunc()">click</button>
<!-- This is the output placeholder -->
<p id="output"></p>
<script src="javascript.js"></script>
</body>
</html>
function myfunc() {
"use strict";
var value = document.getElementById("myid").value;
/* Select the output placeholder */
var outputElement = document.getElementById("output");
/* Set the input fields value as the text content */
outputElement.textContent = value;
}
要在页面上显示该值,可以创建一个空占位符,其ID可以作为目标。然后将此占位符的textContent
设置为输入值。请参见下面的示例
function myfunc() {
"use strict";
console.log(document.getElementById("myid").value)
}
<html>
<head></head>
<body>
<input type="text" id="myid">
<button onclick="myfunc()">click</button>
<!-- This is the output placeholder -->
<p id="output"></p>
<script src="javascript.js"></script>
</body>
</html>
function myfunc() {
"use strict";
var value = document.getElementById("myid").value;
/* Select the output placeholder */
var outputElement = document.getElementById("output");
/* Set the input fields value as the text content */
outputElement.textContent = value;
}
点击
函数myfunc(){
“严格使用”;
var value=document.getElementById(“myid”).value;
/*选择输出占位符*/
var outputElement=document.getElementById(“输出”);
/*将输入字段值设置为文本内容*/
outputElement.textContent=值;
}
如果要打印输入字段的值,可以使用console.log()
将其内容打印到控制台。例如,将输入值保存在变量中。然后,您可以在浏览器的控制台中记录此变量的值,或将其用于其他目的
function myfunc() {
"use strict";
var value = document.getElementById("myid").value;
console.log(value)
}
document.getElementById(“myid”).value
仅获取输入的值。但你什么都没做
您也可以直接记录该值,而无需先将其保存到变量,请参见下面的示例
function myfunc() {
"use strict";
console.log(document.getElementById("myid").value)
}
<html>
<head></head>
<body>
<input type="text" id="myid">
<button onclick="myfunc()">click</button>
<!-- This is the output placeholder -->
<p id="output"></p>
<script src="javascript.js"></script>
</body>
</html>
function myfunc() {
"use strict";
var value = document.getElementById("myid").value;
/* Select the output placeholder */
var outputElement = document.getElementById("output");
/* Set the input fields value as the text content */
outputElement.textContent = value;
}
要在页面上显示该值,可以创建一个空占位符,其ID可以作为目标。然后将此占位符的textContent
设置为输入值。请参见下面的示例
function myfunc() {
"use strict";
console.log(document.getElementById("myid").value)
}
<html>
<head></head>
<body>
<input type="text" id="myid">
<button onclick="myfunc()">click</button>
<!-- This is the output placeholder -->
<p id="output"></p>
<script src="javascript.js"></script>
</body>
</html>
function myfunc() {
"use strict";
var value = document.getElementById("myid").value;
/* Select the output placeholder */
var outputElement = document.getElementById("output");
/* Set the input fields value as the text content */
outputElement.textContent = value;
}
点击
函数myfunc(){
“严格使用”;
var value=document.getElementById(“myid”).value;
/*选择输出占位符*/
var outputElement=document.getElementById(“输出”);
/*将输入字段值设置为文本内容*/
outputElement.textContent=值;
}
如果您希望它在页面中可见,
在HTML中创建一个div并为其设置一个id。然后在JS中获取变量中的值
var myId=document.getElementById(“myId”).value代码>
并在新的div id中设置它
document.getElementById(“printId”).innerHTML=“myId”代码>如果您希望在页面中显示该代码,
在HTML中创建一个div并为其设置一个id。然后在JS中获取变量中的值
var myId=document.getElementById(“myId”).value代码>
并在新的div id中设置它
document.getElementById(“printId”).innerHTML=“myId”代码>您希望它做什么?现在它什么都没做。您正在访问输入字段的值,但没有对其执行任何操作。因此,您在任何地方都看不到输出/反馈。您需要console.log输出打印在哪里?如果您想在控制台中打印,请使用=>console.log(document.getElementById(“myid”).value)
将document.getElementById(“myid”).value
更改为console.log(document.getElementById(“myid”).value)
谢谢大家的努力,我希望js有像print()这样简单的东西或者echo:)你希望它做什么?现在它什么都没做。您正在访问输入字段的值,但没有对其执行任何操作。因此,您在任何地方都看不到输出/反馈。您需要console.log输出打印在哪里?如果您想在控制台中打印,请使用=>console.log(document.getElementById(“myid”).value)
将document.getElementById(“myid”).value
更改为console.log(document.getElementById(“myid”).value)
谢谢大家的努力,我希望js有一些简单的东西,比如print()或echo:)