简体   繁体   English

我无法从我的 html 中的单独文件访问变量

[英]I cant access variable from seperate file in my html

I am trying to populate a dropdown box but I keep getting an undefined error for all currencies when i reference them.我正在尝试填充一个下拉框,但是当我引用所有货币时,我一直收到undefined错误。

This code is given to me from a third party to accomplish the simple goal of populating a dropbox and editing a little text and i have no idea why the variable I need from the referenced js file is coming up undefiened这段代码是由第三方提供给我的,用于完成填充保管箱和编辑一些文本的简单目标,我不知道为什么我需要从引用的js文件中获取的变量没有undefiened

 var allCurrencies = { "AED": "United Arab Emirates Dirham", "BTC": "Bitcoin", "CAD": "Canadian Dollar", "EUR": "Euro", "JPY": "Japanese Yen", "RUB": "Russian Ruble", "USD": "United States Dollar" }; // Initial data for exchange rates var exchangeRates = { "disclaimer": "Usage subject to terms: https://openexchangerates.org/terms", "license": "https://openexchangerates.org/license", "timestamp": 1534107604, "base": "USD", "rates": { "BTC": 0.000157753542, "CAD": 1.316853, "EUR": 0.879353, "JPY": 110.46550427, "USD": 1, } }; /* Your solution goes here */
 <head> <meta charset="UTF-8"> <title>Currency Exchange</title> <script src="jquery-3.2.1.min.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <link href="style.css" rel="stylesheet"> <script src="currex.js" defer></script> <script> </script> </head>

I am just looking for any starting point to help with this我只是在寻找任何起点来帮助解决这个问题

<script src="currex.js" defer></script>

You deferred the loading of the script, so it won't have run before the script element that immediately follows it.您推迟了脚本的加载,因此它不会紧随其后的脚本元素之前运行。

Since it hasn't run yet, the variable isn't defined.由于它尚未运行,因此未定义该变量。

Remove the defer attribute .删除defer属性

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

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