一、操作class
$('.setClass li:first').addClass('red');
$('.setClass li:eq(1)').removeClass('green');
console.log(
$('.setClass li:last').hasClass('green'),
$('.setClass li:last').hasClass('orange'),
);
$('.setClass p').click(function(){
$(this).toggleClass();
});
二、插入元素(内部插入)
$('.insideAdd').append('<h2>append方法插入</h2>');
$('.insideAdd').append($('.insideAdd p'));
$('<h2>appendTo方法插入</h2>').appendTo('.insideAdd');
$('.insideAdd').prepend('<h2>prepend方法插入</h2>');
$('<h2>prependTo方法插入</h2>').prependTo('.insideAdd');
三、插入元素(外部插入,插入为兄弟节点)
$('.outsideAdd h2').after('<p>after方法添加到h2后面</p>');
$('<p>insertAfter方法添加到h2后面</p>').insertAfter('.outsideAdd h2');
$('.outsideAdd h2').before('<p>before方法添加到h2前面</p>');
$('<p>insertBefore方法添加到h2前面</p>').insertBefore('.outsideAdd h2');
四、插入元素,html与text方法。相当于原生的innerHTML、innerText属性
console.log($('.htmlText').html());
$('.htmlText').html('<h2>这是html方法添加的标题</h2><p>这是html方法添加的内容</p>');
console.log($('.htmlText').text());
$('.htmlText').text('<h2>这是text方法添加的标题</h2><p>这是<em>text</em>方法添加的内容</p>');
五、包裹元素
$('.wrap span').wrap('<li>');
$('.wrap li').wrapAll('<ul>');
$('.wrap span').wrapInner('<strong>');
$('.wrap li').unwrap();
六、删除元素
$('div').remove('.title');
$('.del ul').empty();
$('.del .end').click(function(){
alert(1);
});
var end=$('.del .end').detach();
setTimeout(function(){
$('.del').append(end);
},1000);
七、克隆与替换元素
$('.clone p').click(function(){
alert(2);
});
$('.clone p').clone(true).appendTo('.clone');
$('<h3>使用replaceAll方法主动替换</h3>').replaceAll('.clone .replaceAll');
$('.clone .name2').replaceAll('.clone .name1');
$('.clone .replaceWidth').replaceWith('<h3>使用replaceWidth方法被动替换</h3>');
八、属性操作-通用属性操作
console.log($('.attr img').attr('src'));
$('.attr img').attr('title','这是一张美食图片');
$('.attr img').attr({
class:'delicious',
alt:'美食'
});
console.log($('.attr img').prop('src'));
console.log(
$('.attr img').attr('kaivon'),
$('.attr img').prop('kaivon'),
);
$('.attr img').prop({
id:'food',
kaivon:'liuliu',
});
$('.attr img').attr('kaivon','liuliuliu');
$('.attr img').removeAttr('kaivon');
$('.attr img').removeProp('id');
$('.attr img').prop('index',5);
console.log($('.attr img').prop('index'));
$('.attr img').removeProp('index');
console.log($('.attr img').prop('index'));
console.log($('.attr input').val());
$('.attr input').val('这不是一个输入框');
九、属性操作-css类属性操作
console.log(
$('.css').css('border'),
$('.css').css('height'),
);
$('.cssh2').css('width','200px').css('height','100px').css('background','#cc').text('插入一个标题');
$('.css h2').css({
color:'green',
fontSize:'30px',
'line-height':'100px',
});
$('.css p').css({
width:'200px',
height:'200px',
padding:'20px',
margin:'20px auto',
border:'2px solid #f00',
});
console.log(
$('.css p').width(),
$('.css p').height(),
$('.css p').innerWidth(),
$('.css p').innerHeight(),
$('.css p').outerWidth(),
$('.css p').outerHeight(),
);
$('.css p').width(300).height(100).innerWidth(400).outerWidth(500);
$('.css').css('position','relative');
$('.css div').css({
width:'100px',
height:'100px',
background:'green',
position:'absolute',
left:'100px',
top:'200px'
});
console.log(
$('.css div').offset().left,
$('.css div').offset().top,
);
$('.css div').offset({
left:200,
top:1800,
});
console.log(
$('.css div').position().left,
$('.css div').position().top,
);
console.log(
$(document).scrollTop(),
$(document).scrollLeft(),
);
setTimeout(function(){
$(document).scrollTop(300);
},2000);