黑马程序员技术交流社区

标题: javascript中添加java代码? [打印本页]

作者: 抽烟男孩    时间: 2013-5-26 09:58
标题: javascript中添加java代码?
本帖最后由 抽烟男孩 于 2013-5-28 08:23 编辑

在jsp页面中写下如下代码,
  1. <%
  2. String str = "yes";
  3. %>

  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  5. <html>
  6. <head><title>My JSP 'index.jsp' starting page</title>
  7. <script type="text/javascript">
  8. function test(){
  9. <%if(str.equalsIgnoreCase("yes")){%>
  10. alert("yes");
  11. <%} else{%>
  12. alert("no");
  13. <% } %>
  14. }

  15. </script>
  16. </head>

  17. <body>
  18.          <BUTTON   onclick="test()"   >
  19.           测试
  20.          </BUTTON>.

  21. </body>
  22. </html>
复制代码
但是没有任何反应。

作者: 顾玲玉    时间: 2013-5-27 12:55
没有调用你写的方法,有反应就怪了。可以在加载的时候调用下你的方法。<body onload="test();">




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2