java拼接的Html可不可以使用js

printlist.append("<html>");
printlist.append("<head>");
printlist.append("<script type='text/javascript' src='jquery-1.10.2.js'></script>");
printlist.append("<script type='text/javascript'>");
printlist.append("$(function(){");
printlist.append("alert($('#tt').height());");
printlist.append("});");
printlist.append("</script>");
printlist.append("</head>");

有没有方法可以获取到js里面的值到这个类里面用,就好比这个$('#tt').height(),可不可以在这个java类里获取然后使用~
html拼接的可不可以使用js啊??

可以的,直接输出到<script></script>之间就可以了。
步骤:
1、获取response对象
2、获取response的printwriter对象
3、通过printwriter的pring方法输出js
示例代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();//通过servlet的doget方法获取response对象,通过getWriter方法获取PrintWriter对象
out.flush();//清空缓存
out.println("<script>");//输出script标签
out.println("alert('此用户名已存在,请重新输入!');");//js语句:输出alert语句
out.println("history.back();");//js语句:输出网页回退语句
out.println("</script>");//输出script结尾标签
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-09-17

您好,很高兴为您解答!


    能不能,其实是分情况的;


    如果返回的html代码是异步加载,js是修改原来页面的html不可用;


    返回的js是要在页面加载完执行也不可用。


    总是,需要关注两点,一个是js的执行时间,另外是css的渲染时间。


希望我的回答对您有用!

第2个回答  2014-09-16
好像不行,不清楚。为什么不能直接写:
printlist.append("var k = $('#tt').height()");追问

像这种拼接的可不可以使用js?

追答

啥意思?这个var k 你是给谁用 给java代码用 应该做不到。给js用就写在append里面啊

追问

就是拼接读取不到js里面的东西

追答

你直接把代码给我吧,说说要什么效果,我帮你看看

追问

就是报表,拼接表格,数据多了就要换页,每页需要表头和表尾,怎么判断自动换页

代码太多,这里不好复制。

追答

换页你在后头做数据控制啊,比如100条每页20条,你给前台20条,如果用户点击下一页,你在访问后头获取第二页的数据返回20条啊

本回答被提问者采纳
相似回答