简体   繁体   English

从主 js 文件更新多个 html 页面中的文本

[英]update text in multiple html pages from a master js file

So I have a website with multiple pages.所以我有一个包含多个页面的网站。 Each page has one section with exactly the same text that I would like to be able to update on a regular basis from a master.js file which I have called;每个页面都有一个部分,其中包含完全相同的文本,我希望能够从我调用的 master.js 文件中定期更新这些文本; my_info.js我的信息.js

To reference that file I have then placed the following code into an.html page of my website in between the <head></head> tags;为了引用该文件,我将以下代码放入我网站的 an.html 页面的<head></head>标记之间;

<script type="text/javascript" src="my_info.js"></script>

And that's all she wrote.这就是她写的全部内容。 I have no idea what code I need in the my_info.js file.我不知道 my_info.js 文件中需要什么代码。 Nor do I know what code to place within the actual.html pages where I want to display the text from the my_info.js file我也不知道在 actual.html 页面中放置什么代码,我想在其中显示 my_info.js 文件中的文本

This seems such a simple idea I'm sure it must be possible but I've searched the web and I can't find an example/tutorial anywhere.这似乎是一个简单的想法,我确信它一定是可能的,但我已经搜索了 web,但我在任何地方都找不到示例/教程。 When you are answering it's safe to assume that I'm pretty dumb so don't worry about over simplifying things!当您回答时,可以安全地假设我很笨,所以不要担心过度简化事情! Thanks for your help谢谢你的帮助

You'll want to use JavaScript to select the element on the page and set the text of that element.您需要在页面上使用 JavaScript 到 select 元素并设置该元素的文本。 Check out the example below.查看下面的示例。

EDIT: You'll also need to place your <script type="...> code at the bottom of your <body> element just before the closing tag (not in the <head> ) because you want the page to render all the elements before you try and select one with JS.编辑:您还需要将<script type="...>代码放在<body>元素的底部,就在结束标记之前(不在<head>中),因为您希望页面呈现所有内容尝试之前的元素和 select 一个与 JS。

 // my_info.js // Get the element var element = document.getElementById('some-id') // Set the inner text element.innerText = 'This text was inserted by JS'
 <div id="some-id">This text will get overwritten by JS</div>

You should placed <script type="....> in between...body> not in head tag您应该将 <script type="....> 放在...body> 之间,而不是放在 head 标签中

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

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