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覆盖层的添加,方便自己查阅!

时间: 2024-12-11 14:06:58

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

JS 操作Dom节点之样式

为了提高用户体验,我们经常会动态修改Dom节点的样式,各种浏览器差异比较大,我们如何应对?不断尝试,不断总结~! 1. style.getComputedStyle.currentStyle 内嵌样式: <!--body --><div style="width: 30px;background-color: #ff6a00;">我就是傻里傻气的,完全素颜!</div> 1 //内联样式优先级最高,通过style获取的样式是最准确的 2 var el

js 设置多条css样式

如果在一个网页中给id="mydiv"的元素添加css样式,先获取该节点:var obj = document.getElementById("mydiv");给节点添加css:如果需要添加的css不多的话,可以obj.style.width = "300px";如果需要添加多条css语句的话,这样会有多条,如:obj.style.width="300px";obj.style.height="300px"

用JS改变的元素CSS样式

CSS样式的引用有3种方式:style引用.class引用.id引用,所以js改变元素的样式我们也分3种来说. 1.js改变由style方式引用的样式:方法一:document.divs.style.cssText="border:1px solid #000000;color:#FF0000";方法二:document.divs.setAttribute("style","border:1px solid #000000;color:#FF0000&qu

对于用js获取元素的css样式属性(getComputedStyle,currentStyle,style)(getPropertyValue,getAttribute)

首先单独拎出来讲的是style style: nodeObject.style.cssProperty获取的是DOM节点上 style 属性定义的样式,如果不存在 style 属性,或者 style 属性没有定义相应的样式,则是无法获取的.也就是说,JavaScript 不会到 <style> 标签或者 CSS 文件去获取相应的样式,只能获取 style 属性定义的样式. 就是外部样式表,内部样式表,用style都是无效的,只有内联样式是有效的,当然用style写入也是写入在内联样式的. 然后

js获取、修改CSS样式

CSS样式分为三种,行内样式.内部样式.外部样式. 一.element.style.stylename 创建.获取.修改       行内样式   console.log(document.getElementById("testcss0").style.backgroundColor);//"" document.getElementById("testcss0").style.backgroundColor = "black&quo

js之如何获取css样式

一.获取内联样式 1 <div id ="myDiv" style="width:100px;height:100px;background-color:red; border:1px solid black;"></div> 2 <script> 3 var myDiv = document.getElementById("myDiv"); 4 5 alert(myDiv.style.width);//100

用js修改带!important的css样式

普通jQuery修改css的方法或者用dom.style.width="100px"的原生方法修改具有!important表示的样式都无效.查看了w3c标准之后,发现另外一个原生方法可以在修改的时候指定优先级,并且使用之后可行.代码如下: dom.style.setProperty("width","100px","important"); dom.style是一个CSSStyleDeclaration对象,w3c中对它的解释

通过js修改图片的css样式,实现简单的图片旋转

html代码: <img style="max-width: 700px; transform: rotate(360deg);" src="/image/test1.img" class="image1"> <input type="hidden" value="0" name="tran_num1"> <a href="javascript:;

通过js获取元素的css样式属性

var display = this.currentStyle? this.currentStyle.display: window.getComputedStyle(this, null).display; 这样可以获取定义在内部~外部样式表以及内嵌的所有的样式~当然这里指获取display属性~简单来说就是只要定义了display~不管在哪里设置的~都可以获取出来