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

相关文章

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

Nginx更新SSL证书的实现步骤

《Nginx更新SSL证书的实现步骤》本文主要介绍了Nginx更新SSL证书的实现步骤,包括下载新证书、备份旧证书、配置新证书、验证配置及遇到问题时的解决方法,感兴趣的了解一下... 目录1 下载最新的SSL证书文件2 备份旧的SSL证书文件3 配置新证书4 验证配置5 遇到的http://www.cppc

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

SpringBoot整合 Quartz实现定时推送实战指南

《SpringBoot整合Quartz实现定时推送实战指南》文章介绍了SpringBoot中使用Quartz动态定时任务和任务持久化实现多条不确定结束时间并提前N分钟推送的方案,本文结合实例代码给大... 目录前言一、Quartz 是什么?1、核心定位:解决什么问题?2、Quartz 核心组件二、使用步骤1

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

mybatis-plus分表实现案例(附示例代码)

《mybatis-plus分表实现案例(附示例代码)》MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生,:本文主要介绍my... 目录文档说明数据库水平分表思路1. 为什么要水平分表2. 核心设计要点3.基于数据库水平分表注意事项示例