[英]How do I use information from my javascript (const) to insert into my html?
Let's say I have this script:假设我有这个脚本:
const BusInfo = [
{
code: "B31",
destination: "Toronto"
},
{
code: "HJ1",
destination: "Montreal"
}
]
How would I be able to put that information in my html document?我如何才能将该信息放入我的 html 文档中? I cannot change the above script at all.
我根本无法更改上述脚本。 I am having issue doing this, I have tried googling but there isn't much information on how to do that or I am just having trouble comprehending.
我在执行此操作时遇到问题,我曾尝试使用谷歌搜索,但没有太多关于如何执行此操作的信息,或者我只是无法理解。 Also, how would I be able to style this in css?
另外,我如何才能在 css 中设置样式?
I have already called for the script in my index.html, but I am having trouble showing that information.我已经在我的 index.html 中调用了脚本,但我无法显示该信息。 One of the problems am having with is not being able to edit that script at all.
我遇到的问题之一是根本无法编辑该脚本。 I have to keep it like that.
我必须保持这样。
If you are trying to make some kind of project which will be using this kind of html manipulation a lot i would recommend you use a framework for that (react, vue, angular)如果您正在尝试制作某种将大量使用这种 html 操作的项目,我建议您为此使用一个框架(react、vue、angular)
But if its just a one case scenario you can also do it with vanilla js using createElement:但是,如果它只是一个案例场景,您也可以使用 createElement 使用 vanilla js 来完成:
https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement https://developer.mozilla.org/zh-CN/docs/Web/API/Document/createElement
Without more information on the code and what you are trying to achieve this is the best i can give you如果没有更多关于代码的信息和你想要实现的目标,这是我能给你的最好的
Let's say you have a paragraph: <p id="destination">Destination</p>
假设您有一个段落:
<p id="destination">Destination</p>
You could do this to change the content of the paragraph:您可以这样做来更改段落的内容:
document.querySelector('#destination').textContent = BusInfo.destination;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.