2.编写一个网页,实现鼠标移上文本框颜色发生变化效果。文本框得到焦点时背景颜色为“#D4D0C8”,边框为红色;文本框失去焦点时背景颜色为 “#ff千”,边框为蓝色,当鼠标移上按钮时,按钮背景图片发生改变
<!DOCTYPE html>
<html>
<head>
<title>Color Change</title>
<style>
/* 定义文本框样式 */
input[type="text"] {
width: 200px;
height: 30px;
border: 1px solid #ccc;
background-color: #fff;
padding: 5px;
font-size: 16px;
}
/* 定义按钮样式 */
input[type="button"] {
background-image: url('button-image1.jpg');
width: 100px;
height: 40px;
border: none;
cursor: pointer;
}
</style>
<script>
window.onload = function() {
// 获取文本框和按钮的DOM元素
var textBox = document.getElementById("textBox");
var button = document.getElementById("button");
// 给文本框绑定事件,当文本框得到焦点时改变文本框的背景色和边框颜色
textBox.onfocus = function() {
textBox.style.backgroundColor = "#D4DOC8";
textBox.style.border = "1px solid red";
}
// 给文本框绑定事件,当文本框失去焦点时改变文本框的背景色和边框颜色
textBox.onblur = function() {
textBox.style.backgroundColor = "#fff";
textBox.style.border = "1px solid blue";
}
// 给按钮绑定事件,当鼠标移上按钮时改变按钮的背景图片
button.onmouseover = function() {
button.style.backgroundImage = "url('button-image2.jpg')";
}
// 给按钮绑定事件,当鼠标移出按钮时改变按钮的背景图片
button.onmouseout = function() {
button.style.backgroundImage = "url('button-image1.jpg')";
}
}
</script>
</head>
<body>
<label for="textBox">Text Box:</label>
<input type="text" id="textBox">
<br><br>
<input type="button" id="button" value="Button">
</body>
</html>