How can I embed an existing multi-level drop down

2019-07-29 23:13发布

问题:

I have a multi-level drop down menu (done using HTML + CSS) that I want to put on a number of different pages. In the future I will need to update this menu and change its contents, so I have saved the HTML in its own file so that I can roll out the changes to all the pages at once (instead of having to go through each page and repeatedly paste in the changed list items).

I have tried using iframe, but this cuts off the menu items with its limited height (setting a manual height that's big enough would leave tons of blank space, of course):

<iframe height="100%" src="menu.html" frameborder="no" width="100%" scrolling="no"></iframe>

I also tried using embed (this looks fine until you mouse over the menu items -- it just scrolls within the frame):

<embed type="text/html" src="menu.html" width="100%" height="100%"></embed>

The menu functions fine when the code is simply dumped into the individual pages I need it on, so I know that's not the issue. It's the embedding and calling it from its own HTML file that is the problem. Is there a simple way to do this that will allow the drop-down menu to appear as it should?

I should mention that while I have my IT department's blessing to do this, this is a project that they aren't supporting. I can only edit the HTML of my webpages in the body, and not the head. The exception being HTML pages I upload as files (like the menu code). So there are some constraints.

回答1:

Well here is a bit of a long winded javascript approach that might keep your IT guys happy:

window.onload = new Function("load('embed-me.html','content')"); // Replace with URL of your file and ID of div you want to load into.

function ahah(url, target) {
  document.getElementById(target).innerHTML = ' Fetching data...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

function load(name, div) {
    ahah(name,div);
    return false;
}

Not written by me(LINK) (I just added the run on page load bit).

Tested and working (in Chrome at least). Though your site will have no menu if the user has javascript disabled!

EDIT:

Example...

<body>
    <script type="text/javascript" src="embed-me.js"></script> <!-- load the javascript -->
    <div id="content"></div> <!-- html will be embedded here -->
</body>


回答2:

I use the following php code and works very nice. It doesn't even show when you check the source code online.

    <?php include("menu.php"); ?>


回答3:

Use php Include !!

Okay first.. copy the menu code and save it on to a file called menu-1.php

then whenever you want to use your menu; just type the following code:

<?php include("menu-1.php"); ?>

This is a good way to do menu's because every time you need to update your menu, you wont have to update every single page, just update your menu-1.php

P.S. PHP might not show up on your local machine unless you are using wamp or xamp