点击button获取text内容并改变样式的js实现

时间:2022-06-24 02:57:46

需求:点击button获得input框中选中的内容,让选中的内容变红,

实现:代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE HTML>
 
<html>
 
<head>
 
<meta http-equiv="content-type" content="text/html;charset=utf-8">
 
<title>Test</title>
 
<style type="text/css">
 
div{
 
display: none;
 
}
 
</style>
 
</head>
 
<body>
 
<input type="text" id="txt" value="" />
 
<input type="button" value="获取文本框内的值" id="btn" onclick="getText();"/>
 
<div id="showText">
 
</div>
 
<script type="text/javascript">
 
function getText () {
 
var showText = document.getElementById("showText");
 
showText.style.display = "block";
 
showText.style.color = "red";
 
showText.innerHTML=document.getElementById("txt").value;
 
}
 
</script>
 
</body>
 
</html>

效果:

点击button获取text内容并改变样式的js实现