温馨提示×

html中button标签的onclick属性赋值实现方法

清风
6274
2021-03-24 19:17:52
栏目: 编程语言

html中button标签的onclick属性赋值实现方法:1.创建html文件;2.在html文件中添加html架构代码;3.在html架构中的body标签里面使用button标签设置一个按钮并添加onclick属性事件;4.在html架构中的html标签里面添加script标签并添加函数实现赋值;5.通过浏览器方式查看设置效果。

html中button标签的onclick属性赋值实现方法

html中button标签的onclick属性赋值实现方法:

1.首先创建一个html文件。

2.将html架构代码添加到html文件中。

<!DOCTYPE html>

<html>

    <head>

        <title>button标签使用onclick属性示例</title>

    </head>

    <body>

    </body>

</html>

3.在html架构中的body标签里面使用button标签设置一个按钮并添加onclick属性事件即可。

<button onclick="copyText()">复制文本</button>

4.再在html架构中的html标签里面添加script标签并添加一个函数实现赋值。

<script>

function copyText()

{

document.getElementById("field2").value=document.getElementById("field1").value;

}

</script>

5.最后可通过浏览器方式阅读html文件查看设置效果。

完整示例代码如下:

<!DOCTYPE html>

<html>

<head>

<title>button标签使用onclick属性示例</title>

<script>

function copyText()

{

document.getElementById("field2").value=document.getElementById("field1").value;

}

</script>

</head>

<body>

Field1: <input type="text" id="field1" value="Hello World!"><br>

Field2: <input type="text" id="field2"><br><br>

<button onclick="copyText()">复制文本</button>

<p>当按钮被单击时触发函数。此函数把文本从 Field1 复制到 Field2 中。</p>

</body>

</html>

0