简体   繁体   English

从html文件中的js文件更新变量

[英]updating variable from js file in an html file

I have some js code that has been imported from another file which has a variable in it I want to update in my HTML file. 我有一些从另一个文件中导入的js代码,其中包含一个变量,我想在我的HTML文件中进行更新。 How can I update the variable without pasting the js code into my HTML file? 如何在不将js代码粘贴到HTML文件中的情况下更新变量?

JS (script.js): JS(script.js):

var link = "https://example.com"
var codeBlock = `...some html code... ${link} ...some more html code`;

document.getElementById("div").innerHTML = codeBlock

HTML: HTML:

<div id="div"></div>
<script src= "script.js"></script>
<script>
    //I want to change this variable
    var link = "https://anotherexample.com"
</script>

On line one of your code, you declare link and give it a value. 在代码的第一行,您声明link并为其赋值。

On line two, you use that value. 在第二行,使用该值。

You can't change link later on and affect what happened when the value was read and used in the past. 您不能更改link 以后 ,影响当读取和过去使用的值发生了什么事。

If you want to change the final result, then it is the final result you have to change (ie you need to assign a new value to document.getElementById("div").innerHTML ). 如果要更改最终结果,则必须更改最终结果(即,需要为document.getElementById("div").innerHTML分配新值)。

You are getting the value but not setting it to the variable that the HTML is showing.Is this you are looking for : 您正在获取值,但未将其设置为HTML所显示的变量。

 var updatedLink = document.getElementById("anotherlink").innerHTML; var codeBlock = `...some html code... ${updatedLink} ...some more html code`; document.getElementById("div").innerHTML = codeBlock 
 <div id="div"></div> <div id="anotherlink" style="display:none"></div> <script src= "script.js"></script> <script> var link = "https://anotherexample.com"; document.getElementById("anotherlink").innerHTML = link </script> 

Your script is working and the variable is being changed. 您的脚本正在运行,并且变量正在更改。 If you console.log(link) after declaring it again, you'll see the variable has changed. 如果再次声明它后console.log(link) ,您将看到变量已更改。 If you're expecting the script to execute again, that won't happen without calling it again. 如果您希望脚本再次执行,那么必须再次调用它,否则不会发生。

What you may want to try is iterating through the document looking for something to trigger that statement, for example, links in the document. 您可能想尝试的是遍历文档,寻找触发该语句的内容,例如文档中的链接。

 const links = document.querySelectorAll('a'); var href = []; links.forEach(link => href.push(link.href)); var codeBlock = ''; href.forEach(link => { if (link == "https://stacksnippets.net/anotherexample.com") { codeBlock = `...some html code... ${link} ...some more html code`; document.getElementById('div').innerHTML = codeBlock; } }) 
 <div id="div"><a href="anotherexample.com">link</a></div> 

Or without checking for a specific URL: 或不检查特定的URL:

 const links = document.querySelectorAll('a'); var href = []; links.forEach(link => href.push(link.href)); var codeBlock = ''; href.forEach(link => { codeBlock = `...some html code... ${link} ...some more html code`; document.getElementById('div').innerHTML = codeBlock; }) 
 <div id="div"><a href="anotherexample.com">link</a></div> 

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

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