javascript 使用javascript访问速度变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11290716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access velocity variable with javascript
提问by Jaanus
<head> #set($test = "works")) </head>
<script>
var get = "${test}"; // I also tried using '$test' and "$test" also
alert(get);
</script>
And it alerts out ${test}, but should print works.
它会提醒${test},但应该打印works。
How can I get it to work?
我怎样才能让它工作?
回答by patel.milanb
try this...
试试这个...
#set ($test = "works")
<script type="text/javascript">
var myvar = "${test}";
alert (myvar);
</script>
THIS WORKS SURELY!!!!
这肯定有效!!!!
回答by xdazz
Try var get = "$test";
instead of var get = "${test}";
尝试var get = "$test";
代替var get = "${test}";
回答by mwengstrom
I realize the question is old, but this worked for me:
我意识到这个问题很旧,但这对我有用:
#set ($test = "hi")
<script>
alert("$test");
</script>
Need to include quotes around the variable in the alert since it is a string.
需要在警报中的变量周围包含引号,因为它是一个字符串。
回答by some some
Just use it as in html:
就像在 html 中一样使用它:
<script>
var get = $test;
alert(get);
</script>
回答by mfreitas
I had that problem while using tiles, the only way i got it to work was to use the jstl c:out tag, like:
我在使用瓷砖时遇到了这个问题,我让它工作的唯一方法是使用 jstl c:out 标签,例如:
var get = "<c:out value='${test}' />";
that should work, and remember to add the jstl include on the top of the page
这应该可行,并记得在页面顶部添加 jstl 包含
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>