Based operational attribute distal -JQuery

Chapter 5 JQuery action property

5.1 attr operation

  • Providing a single property
// 第一个参数:需要设置的属性名
// 第二个参数:对应的属性值
// $obj.attr(name, value);
// 用法举例
$('img').attr('title','哎哟,不错哦');
$('img').attr('alt','哎哟,不错哦');
  • Set multiple properties
// 参数是一个对象,包含了需要设置的属性名和属性值
// $obj.attr(obj)
// 用法举例
$('img').attr({
    title:'哎哟,不错哦',
    alt:'哎哟,不错哦',
    style:'opacity:.5'
});
  • Acquiring property
// 传需要获取的属性名称,返回对应的属性值
// $obj.attr(name)
// 用法举例
var oTitle = $('img').attr('title');
alert(oTitle);
  • Removing property
// 参数:需要移除的属性名,
// $obj.removeAttr(name);
// 用法举例
$('img').removeAttr('title');

5.2 prop Operation

  • Support After jQuery1.6, for checked, selected, disabled boolean types of properties such, it can not be used attr method, the method can only be used prop.
// 设置属性
$(':checked').prop('checked',true);
// 获取属性
$(':checked').prop('checked');// 返回true或者false

5.3 val () / text () / html () value of the operation

$obj.val()		获取或者设置表单元素的value属性的值
$obj.html() 	对应innerHTML
$obj.text()		对应innerText
以上三个方法:不传参数 表示获取值; 传递一个参数值,表示设置

5.4 class operation

  • Add Style class
// name:需要添加的样式类名,注意参数不要带点.
// $obj.addClass(name);
// 例子,给所有的div添加one的样式。
$('div').addClass('one');
  • Remove the style class
// name:需要移除的样式类名
// $obj.removeClass('name');
// 例子,移除div中one的样式类名
$('div').removeClass('one');
  • Determine whether there is a style class
// name:用于判断的样式类名,返回值为true false
// $obj.hasClass(name)
// 例子,判断第一个div是否有one的样式类
$('div').hasClass('one');
  • Switching style class
// name:需要切换的样式类名,如果有,移除该样式,如果没有,添加该样式。
// $obj.toggleClass(name);
// 例子
$('div').toggleClass('one');

5.5 Implicit Iteration

  1. When the setting operation, if a plurality of elements, then set the same value to all the elements

  2. Get operation time, and if more than one element, then only the first element of the return value.

Released 1800 original articles · won praise 1922 · Views 170,000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/105115226