一、操作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);