jsp导入& js变量
是否可以将 c:import 输出到 Javascript 变量中? 我有一个 file.jsp,其中包含一个 html 对象,我需要将其附加到各个位置,大致如下:
<script>
var element = <c:import url="file.jsp"/>;
$("body").append($element);
</script>
我确实明白这很可能是因为我需要以某种方式包含/转义导入的文件,但我可以不知道该怎么做。
有什么建议吗?
is it possible to output c:import into a Javascript variable?
I have a file.jsp, which contains an html object that I need to append to various places, something along the lines of:
<script>
var element = <c:import url="file.jsp"/>;
$("body").append($element);
</script>
I do understand that this is most likely because I need to somehow contain / escape the imported file, but I can't figure out how to do it.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
file.jsp 的内容是否会根据其在页面上的加载位置而变化?为什么不通过回调来完成同样的事情呢?
例如:
$.get('file.jsp', function(data) {
$('body').append(数据);
});
当然,如果多次使用相同的内容,则需要实施某种缓存。
Does the contents of file.jsp change depending on where it's being loaded on the page? Why not accomplish the same thing with a callback?
For example:
$.get('file.jsp', function(data) {
$('body').append(data);
});
And of course if it's the same content being used many times implement some sort of caching.
我试图为 JavaScript 寻找一种方法,不幸的是我未能找到解决方案。我希望有人可以建议使用这种方式,我也想看看如何做到这一点。
作为替代解决方案,您可以将
file.jsp
的内容加载到默认隐藏
的div
中。然后获取其innerHTML 并将其附加到您的HTML 中。HTML
JS
I have tried to find a way for JavaScript, unfortunately I failed to find a solution. I hope someone can suggest using this way I also want to see how to to this.
As an alternative solution, you can load the content of
file.jsp
intodiv
whichhidden
by default. Then get its innerHTML and append it to your HTML.HTML
JS