[英]JavaScript change background by entering the color name in the textbox
這段代碼似乎有些誤會。 應該發生的是將輸入寫到其中一個文本框,並連接到旁邊的按鈕。 按下按鈕后,div的背景或整個文本將被更改。 我在想輸入框的連接有問題,但無法完全解決。 如何獲取文本框以更改背景或文本?
<style>
#main
{
background: red;
}
</style>
</head>
<body>
<!-- <h1></h1> -->
<input type='text' id='txt1'><button onclick='colour()'>background</button><br>
<input type='text' id='txt2'><button onclick='text()'>text</button>
<div id="main">
<p>
Phasellus at, condimentum nisl. Sed molestie nec velit euismod convallis.
</p>
</div>
<script>
var toDiv = document.getElementById('main');
function colour()
{
var colour2 = document.getElementById('txt1');
toDiv.style.background = (colour2);
}
function text()
{
var texting = document.getElementById('txt2');
main.innerHTML = (texting);
}
</script>
嘗試在末尾添加.value
,例如:
var colour2 = document.getElementById('txt1').value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.