简体   繁体   English

如何将单选项标签数据添加到其他 HTML 页面?

[英]How to add single option tag data to other HTML pages?

For example:例如:

**index.html**
<body>
<option><select>1></select>
<select>2</select><select>3</select></option>
</body>

and having another file:并有另一个文件:

**page1.html**
<body>
here I want to have the same OPTION data
</body>

Is there any way to write the OPTION tag once, and then use it in multiple pages.有没有什么办法把OPTION标签写一次,然后在多个页面中使用。 I have to use them with up to 10 pages.我必须将它们用于最多 10 页。

Sure its possible in javascript hoping that the option values are static and same for all pages.当然它可能在 javascript 中希望所有页面的选项值都是静态的和相同的。

In that case if you have a common javascript file (like helper.js,common.js)在这种情况下,如果您有一个通用的 javascript 文件(如 helper.js、common.js)

Then you include that option code by dynamically binding to the body o your HTML as Kenny mentioned correct you option tag format and check as how to dynamicaly append HTML using javascript然后你通过动态绑定到你的 HTML 的正文来包含该选项代码,正如 Kenny 提到的那样纠正你的选项标签格式并检查如何使用 javascript 动态附加 HTML

Functions to be used : document.createElement() document.body.insertBefore()要使用的函数:document.createElement() document.body.insertBefore()

Reference:参考:

https://developer.mozilla.org/en-US/docs/DOM/document.createElement https://developer.mozilla.org/en-US/docs/DOM/document.createElement

https://developer.mozilla.org/en-US/docs/DOM/Node.insertBefore https://developer.mozilla.org/en-US/docs/DOM/Node.insertBefore

https://developer.mozilla.org/en-US/docs/DOM/Node.firstChildhttps://developer.mozilla.org/en-US/docs/DOM/Node.firstChild

You can make a php file containing the "html tags".您可以制作一个包含“html 标签”的 php 文件。 Then you can include this php file in the required pages.然后你可以在需要的页面中包含这个 php 文件。

You can also use javascript.您也可以使用 javascript。 Create an option.js file, Use the HTML DOM functions like document.createElement() to create the option and select tags, document.createTextNode() to create a text element.创建一个 option.js 文件,使用像 document.createElement() 这样的 HTML DOM 函数来创建选项和选择标签,使用 document.createTextNode() 来创建一个文本元素。

Then use the appendchild function to append the text node to option tags.然后使用 appendchild 函数将文本节点附加到选项标签。

<script src="option.js"></script> 

Use the above script tag in the pages where you require these options.在需要这些选项的页面中使用上述脚本标记。 That is it!这就对了!

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

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