简体   繁体   English

将DIV的内容存储在另一个文件中的CSS中

[英]Storing the content of a DIV in css in another file

I would like to have a "Recent Activity" stream on the left side of my index.html file. 我想在index.html文件的左侧创建一个“最近活动”流。 I have the following code: 我有以下代码:

<div id="sidebar">
        <div class="box1">
            <div class="title">
                <h2>Recent</h2>
            </div>
            <ul class="style2">
                <li>27 Aug - Conference Meeting</li>
                <li>26 Aug - Website Created</li>
            </ul>
        </div>
    </div>

And basically I'd like to be able to store the contents (the activities) in a seperate file, so that I can import that file into the various pages across my website. 基本上,我希望能够将内容(活动)存储在一个单独的文件中,以便可以将该文件导入网站上各个页面。

I'm very new to CSS and webdesign and I've tried to find the answer on google, but haven't run into any luck. 我对CSS和网页设计非常陌生,我试图在google上找到答案,但是还没有碰到任何运气。 I've been tasked with creating a website for my team's college project. 我的任务是为我的团队的大学项目创建一个网站。

Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

In order to do this, you can use CSS like this: 为了做到这一点,您可以像这样使用CSS:

.box1{content: url(http://www.example.com/test.html)}

see more at Mozilla Dev Mozilla Dev上查看更多

This being said, I highly advise NOT to do it this way. 话虽这么说,我强烈建议要这样做。 This is NOT what CSS is intended for. 不是 CSS的目的。 You better use some kind of include like Server Side or PHP. 您最好使用某种包含,例如服务器端或PHP。 If you don't know coding, you can always use a CMS like WordPress which will make this kind of task a breeze 如果您不了解编码,则可以始终使用WordPress之类的CMS,这将使此类任务变得轻而易举

您可以使用Javascript(或相同的库)在带有活动的文件中读取(或者,如果使用javascript表示,则可以仅导入该文件),然后使用javascript将活动插入DOM的适当部分。

The solution is not stripping out html from an existing file but to build up your html files from different sources using a server sided script language like php. 解决方案不是从现有文件中删除html,而是使用服务器端脚本语言(如php)从不同来源构建html文件。

With php you would have just one page (for example index.php) and include the content the user requests (like home, contact, guestbook, whatever). 使用php时,您只有一页(例如index.php),并包含用户请求的内容(如家庭,联系方式,留言簿等)。

The proper way would be to use a database for storing the information but this is far too difficult for begining with web development. 正确的方法是使用数据库存储信息,但这对于从Web开发开始来说太困难了。

Save the html for the sidebar to a separate html file (sidebar.html). 将侧边栏的html保存到单独的html文件(sidebar.html)。 Rename your index.html to index.php. 将您的index.html重命名为index.php。 Go to the location in index.php where your html code for the sidebar is and delete it and instead just insert this: 转到index.php中用于边栏的html代码所在的位置,并将其删除,而只需插入以下内容:

<?php include "sidebar.html"; ?>

This code includes your html file inside your index.php and your can reuse it on different pages by just including the same sidebar.html on every page where you need it. 此代码将您的html文件包含在index.php中,您只需在需要的每个页面上都包含相同的sidebar.html,就可以在不同的页面上重复使用它。 The benefit is to be able to modify it in just one file if you need to make changes. 好处是,如果您需要进行更改,则只能在一个文件中对其进行修改。

Now here is the disadvantage in this solution: You need php installed on the server your website will be hostet. 现在,这里是此解决方案的缺点:您需要在将要作为主机的服务器上安装php。 Php is a program which interacts with the webserver and everytime a .php file is requested by a browser it will be handed over to php first for parsing the file and doing the includes, database access and many other cool things. Php是一个与网络服务器交互的程序,每当浏览器请求一个.php文件时,它将首先移交给php以解析该文件并执行包含,数据库访问和许多其他有趣的工作。 So in order to use php-files and include html files within other html files you need php installed on your webserver. 因此,为了使用php文件并将html文件包含在其他html文件中,您需要在网络服务器上安装php。 Also if you open your .html file by double clicking you won't be able to see the complete result unless you have a local webserver with php installed on your computer. 另外,如果您通过双击打开.html文件,除非您在计算机上安装了本地php服务器,否则将看不到完整的结果。 That's all free software (apache2 and php) but for a beginner it's not so easy to install it. 那是所有免费软件(apache2和php),但是对于初学者来说,安装它并不是那么容易。 Maybe start with a prebuild package like xampp. 也许从xampp之类的预构建包开始。 (This contains the webserver and php with preconfigured settings to start quickly). (它包含具有预配置设置的Web服务器和php,以便快速启动)。

This topic is pretty basic for php so you will be able to find lots of tutorials on the internet about getting started with php. 这个主题是php的基础知识,因此您可以在互联网上找到很多有关php入门的教程。 Good luck and have fun. 祝好运并玩得开心点。 ;) ;)

In simple words, you can do it with <iframe> tag or with the css Content property but it won't give you possibilities as what JavaScript or PHP can give you. 简而言之,您可以使用<iframe>标记或css Content属性来执行此操作,但它不会像JavaScript或PHP所提供的那样为您提供可能性。
To do it with JavaScript you can view this question . 要使用JavaScript做到这一点,您可以查看此问题

Thank you everyone for your help and assistance. 谢谢大家的帮助和协助。 This has been a learning experience. 这是一种学习经验。 For anyone who has the same issue, here is the solution that I've found using some help from the members who kindly responded to my question. 对于有相同问题的任何人,这是我在对我的问题进行回答的成员的帮助下找到的解决方案。

Here is my css: 这是我的CSS:

    <div id="sidebar">
        <div class="box1">
            <div class="title">
                <h2>Recent</h2>
            </div>
            <script type="text/javascript" src="js/recent.js"> </script>
        </div>

        <div class="box2">
            <div class="title">
                <h2>Relevant Links</h2>
            </div>
            <script type="text/javascript" src="js/links.js"> </script>
        </div> 
    </div>

And here are my two *.js files: 这是我的两个* .js文件:

// recent.js - Create recent activity feed
document.write("<ul class=\"style2\">");
document.write("<li>29 Aug - <a href=\"photos.html\">10 Photos Added</a></li>");
document.write("<li>28 Aug - <a href=\"meeting.html\">Meeting Page Added</a></li>");
document.write("<li>27 Aug - Meeting</li>");
document.write("<li>26 Aug - Website Created</li>");
document.write("</ul>");


// links.js - Create list of links
document.write("<ul class=\"style2\">");
document.write("<li><a href=\"http://students.sae.org/cds/formulaseries/\">Formula SAE&reg;</a></li>");
document.write("<li><a href=\"http://www.sae.org/\">SAE International</a></li>");
document.write("</ul>");

It may not be the prettiest way to do it, but it seems to work. 这可能不是最漂亮的方法,但似乎可行。 The problem with the iframes was that if I clicked on a link, it opened it within the iframe. iframe的问题在于,如果我单击链接,则会在iframe中将其打开。 Thanks again everyone! 再次感谢大家! I knew nothing about javascript, css, and it's been a learning experience! 我对javascript,css一无所知,这是一次学习的经历! :) :)

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

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