2016-11-11 50 views
-4

下面是HTML代碼我的JavaScript不工作

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <script src="hello.js"></script> 
    <title>Title</title> 
</head> 
<body> 
    <form action="#"> 
    <p>Name:<input type="text"/></p> 
    <p>Password:<input type="text"/></p> 
    <p><input id= "newsletter_button"type="button" value="CLICK ME"/></p> 

    </form> 

</body> 
</html> 

這是JavaScript:

$(":text").focusin(function() { 
    $(this).css("background-color", "red"); 
}); 
$(":text").blur(function() { 
    $(this).css("background-color", "#fff"); 
}); 

hello.js和index.html是在同一個文件中。我認爲jQuery中存在一些問題。

+2

我跑的代碼,並沒有看到任何問題。你實際上沒有解釋問題是什麼。什麼不適合你? –

+3

這對於一個標題來說是一個非常糟糕的選擇。 – trincot

+0

^^和問題。 –

回答

-1

要麼把你的hello.js在年底前</body>

$(document).ready(function(){ 
$(":text").focusin(function() { 
    $(this).css("background-color", "red"); 
}); 
$(":text").blur(function() { 
    $(this).css("background-color", "#fff"); 
}); 
}); 

它的作用是執行的代碼,一旦整個頁面加載包裹。否則,如果你把它放在「:text」DOM對象加載之前,它會遇到DOM對象未定義的錯誤。

而且你有一個標記錯誤

<p><input id= "newsletter_button"type="button" value="CLICK ME"/></p> 

型前放一個空格:

<p><input id= "newsletter_button" type="button" value="CLICK ME"/></p>