<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?opt=1">
<p style="margin: 0px 0px 1.2em !important">jquery是一个函数库,一个js文件,页面用script标签引入这个js文件就可以使用。
$(document).ready(function(){
......
});
$(function(){
......
});
//设置div的样式
$("div").css("width","30px");
$("div").css("height","30px");
$("div").css({fontSize:"30px",color:"red"});
<h5 id="-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">特别注意
<p style="margin: 0px 0px 1.2em !important">选择器获取的多个元素,获取信息获取的是第一个,比如:$(“div”).css(“width”),获取的是第一个div的width。
<h5 id="-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">操作样式类名
// 内部的this指的是原生对象
// 使用jquery对象用 $(this)
})
<h5 id="-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">获取元素的索引值
<p style="margin: 0px 0px 1.2em !important">有时候需要获得匹配元素相对于其同胞元素的索引位置,此时可以用index()方法获取
1
2
4
5
6
$btn.click(function(){
$('#div1').fadeIn(1000,'swing',function(){
alert('done!');
});
});
fadeOut() 淡出
fadeToggle() 切换淡入淡出
hide() 隐藏元素
show() 显示元素
toggle() 切换元素的可见状态
slideDown() 向下展开
slideUp() 向上卷起
slideToggle() 依次展开或卷起某个元素
<h5 id="jquery-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">jquery链式调用
<h5 id="jquery-jquery-jquery-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">jquery对象的方法会在执行完后返回这个jquery对象,所有jquery对象的方法可以连起来写:
var $htm = $('#div1').html();
// 设置html内容
$('#div1').html('添加文字');
<h5 id="2-prop-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">2、prop() 取出或设置某个属性的值
var $src = $('#img1').prop('src');
// 设置图片的地址和alt属性
$('#img1').prop({src: "test.jpg",alt: "Test Image" });
<h3 id="jquery-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em">jquery循环
<p style="margin: 0px 0px 1.2em !important">对jquery选择的对象集合分别进行操作,需要用到jquery循环操作,此时可以用对象上的each方法:
});
<h3 id="-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em">事件冒泡
<h5 id="-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">什么是事件冒泡
<p style="margin: 0px 0px 1.2em !important">在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个事件会向这个对象的父级对象传播,从里到外,直至它被处理(父级对象所有同类事件都将被激活),或者它到达了对象层次的最顶层,即document对象(有些浏览器是window)。
<h5 id="-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">事件冒泡的作用
<p style="margin: 0px 0px 1.2em !important">事件冒泡允许多个操作被集中处理(把事件处理器添加到一个父级元素上,避免把事件处理器添加到多个子级元素上),它还可以让你在对象层的不同级别捕获事件。
<h5 id="-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">阻止事件冒泡
<p style="margin: 0px 0px 1.2em !important">事件冒泡机制有时候是不需要的,需要阻止掉,通过 event.stopPropagation() 来阻止
});
$(document).click(function(event) {
alert('grandfather');
});
})
......
<div class="father">
<div class="son">
<div class="grandson">
// 合并写法:
return false;
<h3 id="-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em">事件委托
<p style="margin: 0px 0px 1.2em !important">事件委托就是利用冒泡的原理,把事件加到父级上,通过判断事件来源的子集,执行相应的操作,事件委托首先可以极大减少事件绑定次数,提高性能;其次可以让新加入的子元素也可以拥有相同的操作。
<h5 id="-" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">一般绑定事件的写法
1
2
3
4
5
1
2
3
4
5
');
var $div2 = $('这是一个div元素 ');
这是一个span元素');
$('#div1').append($span);
......
<h5 id="jsonp" style="margin: 1.3em 0px 1em; padding: 0px; font-weight: bold; font-size: 1em">jsonp
<p style="margin: 0px 0px 1.2em !important">ajax只能请求同一个域下的数据或资源,有时候需要跨域请求数据,就需要用到jsonp技术,jsonp可以跨域请求数据,它的原理主要是利用了
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|