简体   繁体   English

如何使用 Javascript 制作动态 html 弹出窗口?

[英]How to make dynamic html Pop-UP using Javascript?

I'm working on a school project for a digital menu for a restaurant.我正在为一家餐厅设计数字菜单的学校项目。 What I've done so far is this Pop-UP:到目前为止我所做的是这个弹出窗口: 在此处输入图片说明

It shows up when a button is pressed and I also added an 'X' icon to close it.它在按下按钮时显示,我还添加了一个“X”图标来关闭它。 The problem is that it's static and this is how I store it inside the index.html file:问题是它是静态的,这就是我将它存储在 index.html 文件中的方式:

<div class="menu">
<h2>Our Menu</h2>
<ul>
     <li>
         <label>
             <input type="checkbox" name="">
             <span class="icon"></span>
             <span class="list">Fried Fish With Souce</span>
         </label>
     </li>
</ul>
</div>

As you can see, the data is stored in a static way and the problem is that I must have Pop-UPS for a lot of other categories.如您所见,数据以静态方式存储,问题是我必须为许多其他类别设置 Pop-UPS。 A solution would be to use Javascript to create the Pop-UP:一种解决方案是使用 Javascript 来创建弹出窗口:

function showMenu()
{
    const storedMenu = [{name: "Fried Fish With Souce", checked: false}, {name:"anotherName", checked: false}];
        var content=`<div class="menu"><h2>Our Menu</h2><a href="javascript:closeMenu()" class="close-icon"></a>`;
 
        storedMenu.reduce((accumulator, currentItem) => {
        accumulator += `<ul><li><label> 
        <input type="checkbox" name="">
        <span class="icon"></span>
        <span class="list">${currentItem.name}</span>
        </label><li><ul>`;content+=accumulator;
        }, '' );
            content+="</div>";
            document.write(content);
}

This is the OUTPUT from the source code above:这是上面源代码的输出:

在此处输入图片说明

The problem is that document.write seems to create a whole new page and thus, the CSS doesn't get applied, it OVERWRITES the index.html!问题是 document.write 似乎创建了一个全新的页面,因此没有应用 CSS,它覆盖了 index.html! My question is, how can I create the Dynamic Pop-UP using Javascript in this case but retaining the CSS, also the Pop-UP should simply appear on top of the index.html, but it should still be visible.Thanks in advance!我的问题是,在这种情况下,如何使用 Javascript 创建动态弹出窗口但保留 CSS,弹出窗口应该只出现在 index.html 的顶部,但它仍然应该是可见的。提前谢谢!

document.write only works during rendering of the page. document.write 仅在页面呈现期间有效。 If you want dynamic lists, then use (example)如果您想要动态列表,请使用(示例)

<div id="docwrite"></div>

<script>
var str = "<ul><li>abc</li><li>klm</li><li>xyz</li></ul>";
document.querySelector("#docwrite").innerHTML = str;
</script>

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

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