简体   繁体   English

如何将 javascript 变量传递给 javascript src 链接

[英]How to pass javascript variable to javascript src link

How to pass javascript variable to javascript src link如何将 javascript 变量传递给 javascript src 链接

I want to pass variable myoutput to replace YOUR_LABEL_HERE with variable value我想通过变量 myoutput 用变量值替换 YOUR_LABEL_HERE

My code:我的代码:

 <script>
    var myoutput = 'Diet';
</script>
<script src="/feeds/posts/default/-/YOUR_LABEL_HERE?published&amp;alt=json-in-script&amp;callback=labelthumbs" type='text/javascript'> </script>    

Give it an id , which then allows you to target it like any other element, then change its src :给它一个id ,然后你就可以像任何其他元素一样定位它,然后改变它的src

(inspect the snippet's result to see the change) (检查代码段的结果以查看更改)

 const theScript = document.getElementById("changeMySrc"), theVariable = "something" theScript.src = "http://somesite.com/" + theVariable + "/urlEnd"
 <script id="changeMySrc"></script>

The script is now <script id="changeMySrc" src="http://somesite.com/something/urlEnd"></script> .该脚本现在是<script id="changeMySrc" src="http://somesite.com/something/urlEnd"></script>

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

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