简体   繁体   English

在thymeleaf th:src中使用javascript

[英]Using javascript in thymeleaf th:src

I'm trying to do something like this: 我正在尝试做这样的事情:

 <iframe th:src="'myFunction($videoUrl);'"></iframe>

where myFunction is a jQuery function. 其中myFunction是jQuery函数。 I want the src value to be the output of the function. 我希望src值是函数的输出。

So far, I've tried 到目前为止,我已经尝试过

<iframe th:src="'javascript:myFunction(\''+ ${videoUrl} +'\');'" ></iframe>

However the output is not the result of the function, but the function with the url as attributeL. 但是,输出不是函数的结果,而是url为attributeL的函数。 I'm guessing this works well with onClick, but it's not what I'm looking for. 我猜想这对onClick很有效,但这不是我想要的。

Is it possible? 可能吗?

Uhh... thymeleaf/javascript just don't work this way. 呃... thymeleaf / javascript不能这样工作。 Instead of trying to set the source directly with a javascript function, you should be doing this in a document ready block. 而不是尝试直接使用javascript函数设置源,您应该在文档就绪块中执行此操作。 Something like this for example: 例如:

<script type="text/javascript" th:inline="javascript">
  $(function() {
    var url = [[${videoUrl}]];
    $('#video').attr('src', myFunction(url));
  });
<script>

<iframe id="video"></iframe>

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

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