我遇到了事件添加事件监听器功能的问题,它不会给出错误,它只是不起作用我什至听了复制和粘贴其他代码的视频来工作并修改我的 html 以使用,但它添加了事件监听器我的代码在这里不起作用
<!DOCTYPE html>
<html>
<head><title>Main page of a simple website</title>
<link rel="stylesheet" type="text/css" href="mstn.css"/>
<link href='https://fonts.googleapis.com/css?family=Josefin+Slab:100' rel='stylesheet' type='text/css'>
</head>
<body>
<header>
<a href="https://www.google.com" target="_parent">click here to go to google</a>
<a href="victimclient.rar" download="victimclient.rar">here</a>
<p id="demo">testing testing testing </p>
<p>testing testing testing</p>
<h1>whats little g</h1>
<script type="text/javascript" src="checkthisout.js">
y = document.getElementById("input").addEventListener("click", myFunction);
if(y){
function myFunction() {
alert("hello world")
}
}
</script>
<input holder="password" value="replace here" id="input"/>
</body>
最佳答案
<!DOCTYPE html>
<html>
<head><title>Main page of a simple website</title>
<link rel="stylesheet" type="text/css" href="mstn.css"/>
<link href='https://fonts.googleapis.com/css?family=Josefin+Slab:100' rel='stylesheet' type='text/css'>
</head>
<body onload="init()">
<header>
<a href="https://www.google.com" target="_parent">click here to go to google</a>
<a href="victimclient.rar" download="victimclient.rar">here</a>
<p id="demo">testing testing testing </p>
<p>testing testing testing</p>
<h1>whats little g</h1>
<input holder="password" value="replace here" id="input"/>
<script type="text/javascript">
function init() {
document.getElementById("input").addEventListener("click", myFunction);
}
function myFunction() {
alert("hello world")
}
</script>
</body>
这是解决方案。
在操作 DOM 之前,您必须等待 DOM 被加载。
此外,此 script
标记中不能有 src
属性。
关于javascript - 添加事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39877032/