[英]How to make a speicific text value respond with different text
當我寫“h”時,我試圖取回特定的值,但它不會,我不知道為什么。 我試過尋找問題,但找不到解決方案,我不知道我是不是愚蠢。
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>BOING</title>
<script type="text/javascript">
function res() {
var a = getElementById("a").value;
var c;
if ( a == f ) {
c = "Hello";
}
else if ( a == j) {
c = "Hi";
}
c = document.querySelector("bleh").value;
}
</script>
</head>
<body class="bd" >
<form class="form" action="index.html">
<input id="a" type="text" name="h" pattern="Write Here" />
<button id="b" type="button" name="button" onclick="res()">Ask</button>
</form>
<div class="blah" id="bleh"></div>
</body>
</html>```
如果您想在編寫預定義的消息(如 Hello 或 Hi)時更改 DIV 'bleh' 的文本以響應用戶
f="Hello";j="Hi";
function res() {
var a = document.getElementById("a").value;
var c="";
if ( a == f ) {
c = "Hello";
}
else if ( a == j) {
c = "Hi";
}
document.querySelector("#bleh").innerHTML=c;
}
https://jsfiddle.net/rkv88/kjuox029/11/
修改:
1 要將文本放入 DIV 元素,您必須使用.innerHTML
屬性
2 定義 j & f
您可以使用document.getElemntById("bleh")
而不是document.querySelector("#bleh").innerHTML=c;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.