this的用法:
$(function(){ $('input').click(function(){ //$(this).style.background = 'red'; //$(this)是jquery对象,是当前调用click方法的对象$("input"); this.style.background = 'red'; //this是js对象,$中大部分是指js对象 }); });
js与jquery互转:
$(function(){ //jquery->js $('div')[1].style.background = 'red'; $('div').get(1).style.background = 'red'; //js->jquery var oDiv = document.getElementById('div2'); $(oDiv).css('background','red'); });
has与contains
$(function(){ $('div:has(p)').css('background','red');//div内包含的p标签 $('div:contains(p)').css('background','red');//div内包含的p内容});
val,html,attr:
$(function(){ $('div').html('123');//非表单元素 $('input').val('1111');//表单元素 $('#box').attr({'lot':'let','money':'less'});//属性 });
选择器:
$(function(){ //$('#div1').css('background','aqua'); //$('.red').css('background','pink'); //$('ul li').css('background','white'); //$('ul li').css('background','banana'); //$('ul li:first').css('background','orange'); //$('ul li:last').css('background','orange'); //$('ul li:even').css('background','green'); //$('ul li:eq(2)').css('background','green'); $('ul li:lt(2)').css('background','green'); $('li[type=true]').css('background','yellow'); });
removeClass与addClass:
$(function(){ $('p').removeClass('red').addClass('green'); });
hover里面两个函数(mouseover与moverout):
$(function(){ $('#div1').hover(function(){ $('#div1').css({width:'400px',height:'400px',background:'red'}); },function(){ $('#div1').css({width:'200px',height:'200px',background:'green'}); });});
trim:
$(function(){ var str = ' fd asfa dsa '; alert('|'+$.trim(str)+'|'); //去掉字符串起始和结尾的空格 });
取消冒泡:
$(function(){ $(document).click(function(){ alert('document'); }); $('body').click(function(){ alert('body'); }); $('input').click(function(ev){ alert('input'); return false; //除了取消冒泡外还阻止默认行为 ev.stopPropagation(); //只取消冒泡 }); });
删除元素:
$(function(){ $('#btn').click(function(){ $('#li1').remove(); });});
事件绑定:
$(function(){ $('input').bind('click',function (){alert(1)}); //bind(事件名,函数名) $('input').bind('click mouseover',function (){alert(2)});//可以批量加事件 $('input').unbind('click',fn1);//匿名函数可以绑定但不可以解绑定; });
事件委托:
$(function(){ $('#btn').click(function(){ $('
循环:
$(function(){ $('input').each(function(index,element){ //alert(this == element);//true alert(index); //0,1,2相当于for循环的i }); });
jquery插件:
find方法: