[英]How to change certain characters to other characters in a textarea in html using javascript
[英]Change HTML textarea with JavaScript
我在HTML中有以下代碼:
<button onclick="showDescription();">Find book</button>
在附加的JavaScript文件中:
function showDescription(){
document.getElementById("description").value="book";
}
但是,每當我單擊該按鈕時,它僅顯示字符串“ book” 1秒鍾,然后消失。 知道出了什么問題嗎?
您的按鈕(大概)在<form>
。
按鈕的默認類型是submit
。
不要使用提交按鈕:
<button type="button" onclick="showDescription();">Find book</button>
或者,從事件處理程序返回false:
onclick="showDescription(); return false;"
我猜該按鈕在窗體內,將其更改為:
<button onclick="showDescription();">Find book</button>
至:
<input type="button" onclick="showDescription();" value="Find book" />
按鈕的默認類型為Submit,因此它將提交表單並重新加載頁面(如果它位於form元素內)。
試試這個代碼:
<!DOCTYPE html>
<html>
<head>
<script>
function showDescription()
{
document.getElementById("description").innerHTML="book";
}
</script>
</head>
<body>
<button onclick="showDescription();">Find book</button>
<textarea id="description" rows="4" cols="50">
</textarea>
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.