2016-10-05 272 views
0

我面臨事件添加事件監聽器功能的問題它不會給出錯誤,它只是不工作我甚至聽過視頻複製和粘貼其他代碼工作和修改我的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> 
+0

爲什麼把函數定義放入if條件中?這不是必需的。當添加事件偵聽器時,需要定義**函數,否則不會發生任何事情。 – evolutionxbox

回答

1

<!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操作之前已經加載。 此外,您在此script標記中不能具有src屬性。

+0

爲什麼這是解決方案?請用解釋擴大你的答案。 – evolutionxbox

0

您的腳本在生成輸入之前正在運行,因此可能找不到它。嘗試將腳本放在最後或使用Jquery的document.ready函數。如果內部定義

<!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> 

     <input holder="password" value="replace here" id="input"/> 

<script type="text/javascript" src="checkthisout.js"> 

      function myFunction() { 
       alert("hello world") 
      } 

      document.getElementById("input").addEventListener("click", myFunction); 




     </script> 

    </body> 
+0

爲什麼假設jQuery?問題中沒有jQuery。 – evolutionxbox

+0

這只是另一種可能性。我回答了這個問題,然後提供了另一種方式來做到這一點。 – xboxremote

+0

我明白了,但OP沒有把jquery當作標籤,而且這個問題從來沒有要求過。 – evolutionxbox

0

你的HTML和腳本事項順序,你也不能有功能(Y){},因爲該功能界定及適用範圍尚在「如果」這是不提供給addEventListner水平。試試這個:

<input holder="password" value="replace here" id="input1"/> 

     <script> 

     y = document.getElementById("input1").addEventListener("click", myFunction, false); 

    function myFunction() { 
       alert("hello world") 
      } 

     </script>