2016-11-11 52 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>