由于直接使用dom进行编程很麻烦,API复杂,而且还要考虑浏览器兼容性更各种问题,所以现在诞生了很多框架,老的比如extjs、jquery等,新的有Vue、Angular、React等。Dom需要学,但是还要学习一些的,一来明白底层原理,而来偶尔还会直接操作Dom。
jQuery是风靡了很多年的框架,最近几年有比它更牛逼框架的出现,所以慢慢开始下滑,但是还是要了解一些,因为有一些公司或者老项目还在用jQuery,还要了解一些。本章内容将会做jQuery常用内容的讲解。
jQuery下载地址 http://jquery.com/一定要把jquery.js的引用代码放到用到jquery代码之前。否则会报错:"Uncaught ReferenceError: $ is not defined"
使用DOM给所有div添加一个input
div
{
width:200px;
height:200px;
background-color: green;
margin: 10px;
}
window.οnlοad=function()
{
var divs = document.getElementsByTagName("div");
for(var i=0;i
{
var div = divs[i];
var input = document.createElement("input");
input.type="button";
input.value="按钮";
div.appendChild(input);
}
}
使用jQuery给所有div添加一个input
div
{
width:200px;
height:200px;
background-color: green;
margin: 10px;
}
$(function(){
$("div").append("");
});
$不是什么特殊关键字,因为JavaScript中的标识符是可以包含$的,$是jquery定义的一个函数。这个函数根据传入的数据类型不同,有三个主要用途:
jQuery对象是一个类似于数组的对象,它有链式编程、隐式迭代等特性。
$("div").append("")中的$("div")代表根据标签选择器div获取所有div,因为返回的是jquery对象,代表的是满足条件的多个元素,append就是在每个元素上都调用append方法,把""添加为子节点。这种行为就叫做“隐式迭代”,省的写for循环的麻烦。
jquery提供了很多类似append这样的函数,这些方法大部分都是支持隐式迭代的。
这些方法有一部分会根据参数参数的个数、类型不同而有不同的行为,比如val()就是获取input的value,注意不要丢了()写成val,而val("hello")就是把所有input的value设置为"hello",css("color")是获取color样式的值,css("color","red")则是设置color样式的值为red。对于这些返回值有特殊含义的函数,一般就会返回第一个元素的值,而不是返回每个元素的值。
函数列表:
例子:
.errorMsg
{
color:red;
background-color: yellow;
}
$(function(){
$("input[type=text]").val("hello");
$("input[type=text]").prop("disabled",true);
alert($("#btn1").val());
$("#btn1").val("点我");
alert($("#labelBBall").text());
alert($("#div1").text());
alert($("#div1").html());
$("#labelBBall").html("");
$("#prov").html("");//把prov的innerHtml设置为"",也就是清空
$("div").addClass("errorMsg");
});
足球
篮球
北京
上海
广州
jQuery提供了一系列如click()、focus()、mouseover()等函数,给元素注册对应的事件处理函数。这些函数的名字基本上和dom中的事件的名字一样。
如$("div").click( function(){ alter('xx') }); 这句代码给所有div元素的点击事件注册了处理函数
特别的,jQuery提供了ready()函数来代替window的load加载事件,ready()可以在文档的DOM结构加载完成后就触发,而不必等到页面的图片等资源也加载完成。
特别的,这些函数都可以多次注册回调。
div
{
width:100px;
height:100px;
background-color: green;
margin:10px;
}
$(function(){
$("div").click(function(){
alert($(this).text());
});
$("div").click(function(){
alert($(this).text());
});
});
用click()这种方式注册的监听对于新元素是不起作用,如果想监听新增加的元素,那么要用on函数:on(事件类型,选择器,function),例子
div
{
width:100px;
height:100px;
background-color: green;
margin:10px;
}
$(function(){
$("div").click(function(){
alert("普通click监听 "+$(this).text());
});
$(document).on("click","div",function(){
alert("bind "+$(this).text());
});
$("#btn2").click(function(){
$("
//等价于$("body").append("
});
});
添加Div"/>
因为jQuery的隐式迭代特性,使用jQuery注册的事件处理函数中的this实际上是DOM对象,因此this可以调用DOM API,也可以使用$(this)把Dom对象转换为jQuery对象计行操作。
div
{
width:100px;
height:100px;
background-color: green;
margin:10px;
}
$(function(){
$("div").click(function(){
alert(this.innerText+","+$(this).text());
});
$("#txt1").keypress(function(){
console.log($(this).val());
});
$("#prov").change(function(){
alert($(this).val());
});
});
足球
篮球
北京
上海
广州
jQuery如果函数返回是jquery是可以实现“链式编程”效果的,这样可以简化代码。
如果函数的返回值不再是返回jquery对象,那么就“断链”了。比如val()是获取input的值,返回值是字符串,就不能再继续链式编程下去了。
div
{
width:100px;
height:100px;
background-color: green;
margin:10px;
}
$(function(){
$("#btn1").click(function(){
$("div").css("color","white");
$("div").css("background-color","pink");
$("div").append("");
});
$("#btn2").click(function(){
$("div").css("color","white").css("background-color","pink").append("");
});
});
$(function(){
$("#btnLogin").click(function(){
var username = $("#txtUsername").val();
var password = $("#txtPassword").val();
if(username=="admin"&&password=="123")
{
alert("登录成功");
}
else
{
alert("登录失败");
}
});
});
用户名:
密码:
登录"id="btnLogin"/>
var i=5;
$(function(){
var intId = setInterval(function(){
i--;
if(i==0)
{
$("#agree").prop("disabled",false).val("同意以上协议");
clearInterval(intId);//停止计时器
}
else
{
$("#agree").val("同意以上协议("+i+")");
}
},1000);
});
同意以上协议(5)"disabled/>
$(function(){
var persons =[{id:1,name:'baidu',age:8},{id:2,name:'QQ',age:18},{id:3,name:'淘宝',age:12}];
persons.forEach(function(p){
$("#tb").append(" ");"+p.id+" "+p.name+" "+p.age+"
});
});
id 姓名 年龄
$(function(){
$("#selectAll").click(function(){
$("input[name=site]").prop("checked",true);
});
$("#deSelectAll").click(function(){
$("input[name=site]").prop("checked",false);
});
$("#revSelectAll").click(function(){
$("input[name=site]").each(function(){
$(this).prop("checked",!$(this).prop("checked"));
});
});
});
百度
百度
腾讯
阿里
头条
全选"/>
全不选"/>
反选"/>
#vipArea
{
display:none;
}
$(function(){
$("#isVip").change(function(){
if($(this).prop("checked"))
{
$("#vipArea").show();
}
else
{
$("#vipArea").hide();
}
});
});
会员
卡号:
密码: