JQuery(3)jquery属性操作

无参数就是获得html内容,有参数就是设置html内容

1、html() 取出或设置html内容

// 取出html内容

var $html = $('#div1').html();

// 设置html内容

$('#div1').html('<span>添加文字</span>');

2、text() 取出或设置text内容 

// 取出文本内容

var $html = $('#div1').text();

// 设置文本内容

$('#div1').text('<span>添加文字</span>');

3、attr() 取出或设置某个属性的值

拿取前面元素的src属性  注意是属性

// 取出图片的地址

var $src = $('#img1').attr('src');

// 设置图片的地址和alt属性

$('#img1').attr({ src: "test.jpg", alt: "Test Image" });

猜你喜欢

转载自blog.csdn.net/super_lixiang/article/details/82024343