玩!模板 - 将动态字符串传递给 JavaScript 不起作用?

发布于 2024-12-05 17:52:35 字数 466 浏览 1 评论 0原文

基本上,在控制器中,发生了一些动态的事情,我将一个非静态字符串传递给视图:

String token = "";
render(token);

我可以轻松地执行以下操作:

<div id="token>${token}</div>

...并获取内容:

$('#token').html();

但似乎不起作用的是,在 javascript 中,做:

function token(token) {
  // do something with token
  console.log('token: ' + token);
}

token(${token});

我可以理解为什么它不起作用......但是它的语法是什么?

Basically, in the controller, something dynamic happens, and I pass a non-static string to the view:

String token = "";
render(token);

I can easily do:

<div id="token>${token}</div>

...and grab the contents:

$('#token').html();

But what doesn't seem to work is, in the javascript, doing:

function token(token) {
  // do something with token
  console.log('token: ' + token);
}

token(${token});

I can kind of see why it doesn't work...but what is the syntax for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姜生凉生 2024-12-12 17:52:35

您没有命名您的函数。它应该是:

function token(token) {
  // do something with token
  console.log('token: ' + token);
}
token(${token});

编辑: 您可能只需要添加引号:

token("${token}");

顺便说一句,这可行,但一般来说我会避免使用相同的名称对于函数及其参数。更好的名称可能是 logToken

function logToken(token) {
  // do something with token
  console.log('token: ' + token);
}

logToken("${token}");

You didn't name your function. It should be:

function token(token) {
  // do something with token
  console.log('token: ' + token);
}
token(${token});

Edit: You may need to just add quotes:

token("${token}");

By the way, this works, but in general I'd avoid using the same name for the function and its argument. A better name might be logToken:

function logToken(token) {
  // do something with token
  console.log('token: ' + token);
}

logToken("${token}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文