js实现html节点、CSS样式、事件的动态添加以及html覆盖层的添加

2024-05-16 02:32

本文主要是介绍js实现html节点、CSS样式、事件的动态添加以及html覆盖层的添加,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

(一)js实现html节点、CSS样式、事件的动态添加

   ①场景描述:我们需要动态获取后台数据并将这些数据以列表方式展示,其中列表存在自己的列表样式,每个item都存在自己的点击事件.....那么在这种情况下我们是不是就需要用到动态添加节点的模式去处理呢?

  ②代码记录如下:

$.ajax({url : "***.action",type : 'post',dataType : 'json',contentType : "application/x-www-form-urlencoded; charset=utf-8",data : {'name' : name,'type' : type},success : function(data) {$("#lianxiang").empty();var mydiv = document.getElementById("lianxiang");var arr = new Array();arr = data;if(arr.length==0){document.getElementById('lianxiang').innerHTML='  未找到相关站点或线路';}if (arr.length > 0) {for ( var j = 0; j < arr.length; j++) {var arr_l = new Array();arr_1 = arr[j];var divone = document.createElement("div");if(j%2==0){divone.setAttribute("class","ui-block-a");}else{divone.setAttribute("class","ui-block-b");	}				var divtwo = document.createElement("div");divtwo.setAttribute("class","bus_zp_list_more_01");var aa = document.createElement("a");aa.setAttribute("href","#");var span = document.createElement("span");								span.innerHTML = arr_1[2];divtwo.id = j;aa.appendChild(span);divtwo.appendChild(aa);divone.appendChild(divtwo);mydiv.appendChild(divone);divtwo.onclick = function() {var idnum = $(this).attr('id');ewohobustwo(data[idnum][0], data[idnum][1],data[idnum][2], data[idnum][3]);};}} },error : function() {alert("网络忙,请再次尝试哦!");}});
其中首先找到父节点mydiv ,然后采用 var divone = document.createElement("div")的方式创建新的节点(这里可以使div、span、a等各种),同样可以对新创建的节点添加新的css、点击事件、id等,最后将这些新的节点元素一点点添加到父级元素即可完成动态元素的创建添加;

(二)html覆盖层的添加

①需要在页面引入一下html

//	<div class="DialogDiv" style="display: none;">
//		<div class="U-guodu-box">
//			<div>
//				<table width="100%" cellpadding="0" cellspacing="0" border="0">
//					<tr>
//						<td align="center"><img src="images/loading.gif">
//						</td>
//					</tr>
//					<tr>
//						<td valign="middle" align="center" style="text-shadow: 0 0 0">进行中,请稍后...</td>
//					</tr>
//				</table>
//			</div>
//		</div>
//	</div>

②加入对应的lodeing的css

#BgDiv1{background-color:#000; position:absolute; z-index:9999;  display:none;left:0px; top:0px; width:100%; height:100%;opacity: 0.6; filter: alpha(opacity=60);}
.DialogDiv{position:absolute;z-index:99999;}
.U-user-login-btn{ display:block; border:none; background:url(images/bg_mb_btn1_1.png) repeat-x; font-size:1em; color:#efefef; line-height:49px; cursor:pointer; height:53px; font-weight:bold;
border-radius:3px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;width:100%; box-shadow: 0 1px 4px #cbcacf, 0 0 40px #cbcacf ;}.U-user-login-btn:hover, .U-user-login-btn:active{ display:block; border:none; background:url(images/bg_mb_btn1_1_h.png) repeat-x; font-size:1em; color:#efefef; line-height:49px; cursor:pointer; height:53px; font-weight:bold;
border-radius:3px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;width:100%; box-shadow: 0 1px 4px #cbcacf, 0 0 40px #cbcacf ;}
.U-user-login-btn2{ display:block; border:none;background:url(images/bg_mb_btn1_1_h.png) repeat-x;   font-size:1em; color:#efefef; line-height:49px; cursor:pointer; font-weight:bold;
border-radius:3px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;width:100%; box-shadow: 0 1px 4px #cbcacf, 0 0 40px #cbcacf ;height:53px;}
.U-guodu-box { padding:5px 15px;  background:#3c3c3f; filter:alpha(opacity=90); -moz-opacity:0.9; -khtml-opacity: 0.9; opacity: 0.9;  min-heigh:200px; border-radius:10px;}
.U-guodu-box div{ color:#fff; line-height:20px; font-size:12px; margin:0px auto; height:100%; padding-top:10%; padding-bottom:10%;}

③下面两个js方法实现了对覆盖层的展示和隐藏

	function showlode() {$("#BgDiv1").css({display : "block",height : $(document).height()});var yscroll = document.documentElement.scrollTop;var screenx = $(window).width();var screeny = $(window).height();$(".DialogDiv").css("display", "block");$(".DialogDiv").css("top", yscroll + "px");var DialogDiv_width = $(".DialogDiv").width();var DialogDiv_height = $(".DialogDiv").height();$(".DialogDiv").css("left", (screenx / 2 - DialogDiv_width / 2) + "px");$(".DialogDiv").css("top", (screeny / 2 - DialogDiv_height / 2) + "px");$("body").css("overflow", "hidden");}function hidelode() {$("#BgDiv1").css({display : "none",height : $(document).height()});var yscroll = document.documentElement.scrollTop;var screenx = $(window).width();var screeny = $(window).height();$(".DialogDiv").css("display", "none");$(".DialogDiv").css("top", yscroll + "px");var DialogDiv_width = $(".DialogDiv").width();var DialogDiv_height = $(".DialogDiv").height();$(".DialogDiv").css("left", (screenx / 2 - DialogDiv_width / 2) + "px");$(".DialogDiv").css("top", (screeny / 2 - DialogDiv_height / 2) + "px");$("body").css("overflow", "hidden");}

以上基本记录了js实现html节点、CSS样式、事件的动态添加以及html覆盖层的添加,方便自己查阅,欢迎访问个人博客(http://cuiyongzhi.com)!

这篇关于js实现html节点、CSS样式、事件的动态添加以及html覆盖层的添加的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/993619

相关文章

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll