3-8 jQuery的Dom事件

1.补充说明

ready:当DOM载入就绪可以查询即操作时绑定一个要执行的函数。极大提高web应用程序的响应速度
onload: 是js中的方法,网页的所有标签元素、图片全部加载完毕之后才执行这个事件处理程序,速度相对较慢
例:

//在dom内容载入就绪之后执行
			$(document).ready(function(){
				// $("#d1").css("width: 50%;");
				console.info("1");
				//绑定事件拿到ready里面
				//on()绑定事件
				$("#b1").on("click",function(){
					$(this).css("color","red");
				});
				$("#b1").click(function(){
					$(this).css({
						"color":"red",
						"font-size":"36px"
					});
				});
			});
# 2.绑定操作

## 2.1绑定

//绑定事件拿到ready里面
//on()绑定事件

```java

$("#b1").on(“click”,function(){
$(this).css(“color”,“red”);
});


## 2.2解绑
off()进行函数解绑
```java
$("#b1").off("click");

3.DOM操作

生成元素

var ele = $("<p>hello world</p>");

插入元素

在内部尾部插入

$("div").append(ele);

在内部头部插入

$("div").prepend(ele);

在外部头部插入

ele.insertBefore("#d1");

在外部尾部插入

ele.insertAfter("#d1");

删除元素

删除全部子元素

$("#d1").empty();

删除指定元素及其子元素

$("#d1").remove();

遍历

var arr = [];
arr = $("input");

index:下标/索引,从0开始
item:数组中的元素对象

arr.each(function(index, item){
			console.info(item.value);
});
arr = ["a","b","c"];
				$.each(arr, function(index, item){
					console.info(index+":"+item);
				});

界面样式

<body>	
		
		<div id="d1" style="border:1px solid red;height: 200px;">
			<h1>过年好!!!</h1>
			<span id="s1">拿红包!!!</span>
		</div>
		
		<input type="text" placeholder="请输入姓名" />
		<input type="text" placeholder="请输入年龄" />
		<input type="password" placeholder="请输入密码" />
		
		
		<button id="b1">按钮</button>
		<button onclick="closeOn();">解绑事件</button>
	</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值