$ document.ready()在servlet中不起作用

时间:2013-11-17 08:41:17

标签: javascript jquery css jsp servlets

我在java servlet内使用javascript和html。我使用get方法将参数从jsp页面传递给servlet,因此在servlet中使用doGet方法。 这一行:

out.println("$(document).ready(function{alert('Inside a function')});"); //is not working. 

如果我删除此语句,代码运行正常,即警报1然后警告2。

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out=response.getWriter();
  out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
  out.println("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>");
  out.println("<head>"+"<link href='./styles/styles.css' type= 'text/css' rel='stylesheet'/>");
  out.println("<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js' type='text/javascript'></script>");
  out.println("<script src='http://jquery-star-rating-plugin.googlecode.com/svn/trunk/jquery.rating.js' type='text/javascript' language='javascript'></script>");
  out.println("<link href='http://jquery-star-rating-plugin.googlecode.com/svn/trunk/jquery.rating.css' type='text/css' rel='stylesheet'/>");           

out.println("<script type='text/javascript' language='javascript'>");
out.println("alert('1');");                    out.println("$(document).ready(function{alert('Inside a function')});");             out.println("alert('2');");

out.println("</script>");  //closing the script tag

out.println("</body>"); //closing the body tag

out.println("</html>"); //closing the html tag

out.close();


}

1 个答案:

答案 0 :(得分:1)

试试这个:

 $(document).ready(function(){alert('Inside a function')});