简体   繁体   English

jsp 导入&js变量

[英]jsp import & js variables

is it possible to output c:import into a Javascript variable?是否可以将 output c:import 到 Javascript 变量中? I have a file.jsp, which contains an html object that I need to append to various places, something along the lines of:我有一个文件。jsp,其中包含一个 html object 我需要 Z9516DFB15F51C 的一些东西 C81 到各个地方,B

<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?有什么建议么?

Does the contents of file.jsp change depending on where it's being loaded on the page? file.jsp 的内容是否会根据页面上的加载位置而变化? Why not accomplish the same thing with a callback?为什么不用回调来完成同样的事情呢?

For example:例如:

$.get('file.jsp', function(data) { $('body').append(data); }); $.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.当然,如果多次使用相同的内容,请实施某种缓存。

I have tried to find a way for JavaScript, unfortunately I failed to find a solution.我试图找到 JavaScript 的方法,不幸的是我没有找到解决方案。 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 into div which hidden by default.作为替代解决方案,您可以将file.jsp的内容加载到默认hiddendiv中。 Then get its innerHTML and append it to your HTML.然后将其 innerHTML 和 append 获取到您的 HTML。

HTML HTML

//Your div that hold the Content.jsp
<div id="content" style="display:none">
    <c:import url="Content.jsp" />
</div>

JS JS

 $(document).ready(function(){
     var element = $('#content').html(); //Store the value of content div to a variable         
     $('body').append(element); //append the element to body
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM