Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
根据页面 overlay 的显示状态动态添加 DOM 元素_创想鸟

根据页面 overlay 的显示状态动态添加 DOM 元素

根据页面 overlay 的显示状态动态添加 dom 元素

本文介绍了如何使用 JavaScript 根据页面 overlay 元素的显示状态,动态地向 DOM 中添加新的元素。通过检查 overlay 元素是否包含特定的 class,可以判断其是否显示,从而决定是否创建并插入新的按钮元素。本文提供了详细的代码示例和步骤说明,帮助开发者实现这一功能。

动态添加 DOM 元素

在 Web 开发中,经常需要根据特定条件动态地改变页面的内容。一个常见的场景是,根据某个元素的显示状态,决定是否添加或隐藏其他元素。本文将以一个实际案例为例,讲解如何使用 JavaScript 实现这一功能。

需求描述

假设页面中有一个 overlay 元素,当该 overlay 处于隐藏状态时,需要在页面中添加一个新的按钮元素。如果 overlay 处于显示状态,则不添加该按钮。

实现步骤

获取 overlay 元素: 首先,需要通过 JavaScript 获取到 overlay 元素的引用。可以使用 document.getElementById() 或 document.getElementsByClassName() 等方法来获取元素。

var overlayContainer = document.getElementById("mobile-start-container");var divOverlay = document.getElementsByClassName("mobile-start-overlay")[0];

检查 overlay 的显示状态: 接下来,需要判断 overlay 元素是否处于显示状态。在本例中,可以通过检查 overlay 元素是否包含名为 shown 的 class 来判断。

if (divOverlay.classList.contains('shown')) {    console.log("overlay is shown so don't display the button");}

动态添加按钮元素: 如果 overlay 处于隐藏状态,则需要创建新的按钮元素,并将其添加到 DOM 中。

else {    // Create new Button element    var newButton = document.createElement("button");    // add class to the new Button    newButton.className = "question-btn";    // add SVG tag inside Button    var newSvg = document.createElement("svg");    // add class to svg tag    newSvg.className = "svg-icon";    // fill svg element with my shape    var svgContent = document.createElementNS("http://www.w3.org/2000/svg", 'path');    svgContent.setAttribute("d", "M 0 0 L 10 10");    svgContent.style.stroke = "#fff";    svgContent.style.strokeWidth = "1px";    newSvg.appendChild(svgContent);    // add svg node to created button    newButton.appendChild(newSvg);    // add new button somewhere else in DOM    var currentDiv = document.getElementById('nav-controls');    document.body.insertBefore(newButton, currentDiv);    console.log(newButton);}

上述代码首先创建了一个新的 button 元素,并设置了其 class 属性。然后,创建了一个 svg 元素,并向其中添加了一个 path 元素,用于定义按钮的形状。最后,将 svg 元素添加到 button 元素中,并将 button 元素插入到 document.body 中 id 为 nav-controls 的元素之前。

完整代码示例

document.body.onload = addElement;function addElement() {    var overlayContainer = document.getElementById("mobile-start-container");    console.log(overlayContainer);    var divOverlay = document.getElementsByClassName("mobile-start-overlay")[0];    console.log(divOverlay);    if (divOverlay.classList.contains('shown')) {        console.log("overlay is shown so don't display the button");    } else {        // Create new Button element        var newButton = document.createElement("button");        // add class to the new Button        newButton.className = "question-btn";        // add SVG tag inside Button        var newSvg = document.createElement("svg");        // add class to svg tag        newSvg.className = "svg-icon";        // fill svg element with my shape        var svgContent = document.createElementNS("http://www.w3.org/2000/svg", 'path');        svgContent.setAttribute("d", "M 0 0 L 10 10");        svgContent.style.stroke = "#fff";        svgContent.style.strokeWidth = "1px";        newSvg.appendChild(svgContent);        // add svg node to created button        newButton.appendChild(newSvg);        // add new button somewhere else in DOM        var currentDiv = document.getElementById('nav-controls');        document.body.insertBefore(newButton, currentDiv);        console.log(newButton);    }}

注意事项

确保在 DOM 加载完成后执行 JavaScript 代码,可以使用 document.body.onload 或 DOMContentLoaded 事件来确保代码在页面加载完成后执行。在添加新的 DOM 元素时,需要注意选择合适的插入位置,避免影响页面的布局。可以使用 CSS 来控制新添加元素的样式,使其与页面的整体风格保持一致。如果 overlay 的显示状态发生变化,需要重新执行上述代码,以确保按钮元素的显示状态与 overlay 的显示状态同步。

总结

本文介绍了如何使用 JavaScript 根据页面 overlay 元素的显示状态,动态地向 DOM 中添加新的元素。通过检查 overlay 元素是否包含特定的 class,可以判断其是否显示,从而决定是否创建并插入新的按钮元素。这种方法可以应用于各种需要根据条件动态改变页面内容的场景,提高 Web 应用的灵活性和用户体验。

以上就是根据页面 overlay 的显示状态动态添加 DOM 元素的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1575697.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
使用 JavaScript 根据 DOM 元素的 Class 隐藏元素
上一篇 2025年12月22日 17:22:03
基于 JavaScript 根据 Div 类名隐藏 DOM 元素
下一篇 2025年12月22日 17:22:20

相关推荐

发表回复

登录后才能评论
关注微信