简体   繁体   English

如何使用我的 javascript (const) 中的信息插入我的 html?

[英]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.

相关问题 使用JavaScript如何在我的页面中插入HTML代码? - Using JavaScript how do I insert html code into my page? 如何从我的HTML / JavaScript应用程序中引用jQuery? - How do I reference jQuery from my HTML/JavaScript application? 如何使用可从Javascript访问的HTML发送值? - How do I send a value with my HTML that is accessible from Javascript? 如何通过内容脚本将模板中的 HTML 插入 Chrome 扩展程序中的 div - How do I insert the HTML from a template into my div in my Chrome extension via a content script 如何从 RESTful 接口获取信息并在我的 Javascript 代码中使用它? - How can I GET information from a RESTful interface and use it in my Javascript code? NodeJs:如何从我的 HTML 前端访问我的 JavaScript 后端的功能? - NodeJs: How do I access the functions of my JavaScript backend from my HTML frontend? Javascript-如何将用于查询数据库的诺言将变量发送到前端? - Javascript - How do I send my variable to my frontend from the promise I use to query the database? 在 HTML5 JavaScript 中,如何从 Sqlite 数据库中读取 BLOB 字段并将其用作我的 Z25267A5EC93705EB8 应用程序中的内容? - In HTML5 JavaScript, how do I read a BLOB field from an Sqlite database and use it as content in my web app? 使用Javascript时我的HTML内容不显示 - My HTML content do not display when I use Javascript 如何使用概率并将它们插入我的其他 function? - How do I use probability and insert them into my other function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM