请知悉:本文最近一次更新为 10年 前,文中内容可能已经过时。

很多属性都可以通过setAttribute()来实现,下面整理JavaScript通过setAttribute来修改onclick属性示例代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
input {width:300px;height:50px;line-height:50px;}
</style>
</head>
<body>
<div>
<input id="ebutt" type="button" onclick="alert('原始内容');" value="测试修改onclik事件" />
</div>
<script type="text/JavaScript">
document.getElementById("ebutt").setAttribute("onclick","alert('你点击我了')");
</script>
</body>
</html>

访问上面的html文件,点击发现是弹出了你点击我了的提示,说明原始的onclick已经被修改过了,举一反三,修改其他函数替换alert也是完全可以的。

整理修改自:https://jianzhong5137.blog.163.com/blog/static/9829049201171241034334/


如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《JavaScript修改onclick事件》相关的博文:


留言

avatar
😀
😀😁😂😅😭🤭😋😘🤔😰😱🤪💪👍👎🤝🌹👌