gpt4 book ai didi

javascript - 阅读和书写姓名时出现问题

转载 作者:行者123 更新时间:2023-12-01 02:03:13 24 4
gpt4 key购买 nike

我刚刚学习js,遇到以下问题,但我似乎无法解决。练习的目的是在单击按钮时读取名称,然后显示一条消息“hello”

所有函数都包含在 mainC div 中。

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Sentence Diagrammer</title>
<link rel="stylesheet" href="./css/main.css">
</head>
<body>
<h1>Welcome to the Sentence Diagrammer!</h1>
<h2>You can practice your diagramming skills here.</h2>
<div id="container">
<div id="mainL">
<h1>Left Conatainer</h1>
</div>

<div id="mainC">
<h1>This is the center</h1>
<p>Please tell us your name</p>

<p>I am <input id="theName" name="aName" type="text" /></p>
<button onclick="validName()">Click</button>
<p id="demo"></p>

<input id="slide" type="range" min="1" max="10" step="1" onchange="displayPrelimQ(this.value)" />
<span id="slideValue">1</span>

<script type="text/javascript">
function validName() {
var text1 = "hello";
var text2 = document.getElementById("theName").innerHTML;
document.getElementById("demo").innerHTML = text1 + text2;

}
</script>

<script type="text/javascript">
function displayPrelimQ(val) {
document.getElementById("slideValue").innerHTML = val;

}
</script>
</div>

<div id="mainR">
<h1>right container</h1>
</div>
</div>
</body>
</html>

最佳答案

问题是输入值无法通过 .innerHTML 获得,但是通过.value 。作品如下:

function validName() {
var text1 = "hello";
var text2 = document.getElementById("theName").value;
document.getElementById("demo").innerHTML = text1 + " " + text2;
}

请注意 <input />是一个自闭合标签。 (即它没有相应的 </input> )。因此,它的内部 HTML 没有多大意义,因为它总是空的。

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Sentence Diagrammer</title>
<link rel="stylesheet" href="./css/main.css">
</head>
<body>
<h1>Welcome to the Sentence Diagrammer!</h1>
<h2>You can practice your diagramming skills here.</h2>
<div id="container">
<div id="mainL">
<h1>Left Conatainer</h1>
</div>

<div id="mainC">
<h1>This is the center</h1>
<p>Please tell us your name</p>

<p>I am <input id="theName" name="aName" type="text" /></p>
<button onclick="validName()">Click</button>
<p id="demo"></p>

<input id="slide" type="range" min="1" max="10" step="1" onchange="displayPrelimQ(this.value)" />
<span id="slideValue">1</span>

<script type="text/javascript">
function validName() {
var text1 = "hello";
var text2 = document.getElementById("theName").value;
document.getElementById("demo").innerHTML = text1 + " " + text2;

}
</script>

<script type="text/javascript">
function displayPrelimQ(val) {
document.getElementById("slideValue").innerHTML = val;

}
</script>
</div>

<div id="mainR">
<h1>right container</h1>
</div>
</div>
</body>
</html>

关于javascript - 阅读和书写姓名时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50361734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com