首页 > 编程语言 >深入学习jquery源码之prop()与removeProp()

深入学习jquery源码之prop()与removeProp()

时间:2023-02-23 22:02:41浏览次数:39  
标签:jquery jQuery name function removeProp elem prop 源码 属性


深入学习jquery源码之prop()与removeProp()

prop(name|properties|key,value|fn)

概述

获取在匹配的元素集中的第一个元素的属性值。

随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误。jQuery第一次分配undefined值的属性,而忽略了浏览器生成的任何错误

参数

name String

属性名称

properties Map

作为属性的“名/值对”对象

key,value String,Object

属性名称,属性值

key,function(index, attr) String,Function

1:属性名称。

2:返回属性值的函数,第一个参数为当前元素的索引值,第二个参数为原先的属性值。

选中复选框为true,没选中为false

$("input[type='checkbox']").prop("checked");

禁用页面上的所有复选框。

$("input[type='checkbox']").prop({
disabled: true
});

禁用和选中所有页面上的复选框。

$("input[type='checkbox']").prop("disabled", false);
$("input[type='checkbox']").prop("checked", true);

通过函数来设置所有页面上的复选框被选中。

$("input[type='checkbox']").prop("checked", function( i, val ) {
return !val;
});

 

removeProp(name)

概述

用来删除由.prop()方法设置的属性集

随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误。jQuery第一次分配undefined值的属性,而忽略了浏览器生成的任何错误

参数

propertyName String

要删除的属性名

设置一个段落数字属性,然后将其删除。

<p> </p>
var $para = $("p");
$para.prop("luggageCode", 1234);
$para.append("The secret luggage code is: ", String($para.prop("luggageCode")), ". ");
$para.removeProp("luggageCode");
$para.append("Now the secret luggage code is: ", String($para.prop("luggageCode")), ". ");
The secret luggage code is: 1234. Now the secret luggage code is: undefined.

 

jquery源码

var rfocusable = /^(?:input|select|textarea|button|object)$/i,
rclickable = /^(?:a|area)$/i;

jQuery.fn.extend({
prop: function (name, value) {
return access(this, jQuery.prop, name, value, arguments.length > 1);
},

removeProp: function (name) {
name = jQuery.propFix[name] || name;
return this.each(function () {
// try/catch handles cases where IE balks (such as removing a property on window)
try {
this[name] = undefined;
delete this[name];
} catch (e) { }
});
}
});

jQuery.extend({
propFix: {
"for": "htmlFor",
"class": "className"
},

prop: function (elem, name, value) {
var ret, hooks, notxml,
nType = elem.nodeType;

// don't get/set properties on text, comment and attribute nodes
if (!elem || nType === 3 || nType === 8 || nType === 2) {
return;
}

notxml = nType !== 1 || !jQuery.isXMLDoc(elem);

if (notxml) {
// Fix name and attach hooks
name = jQuery.propFix[name] || name;
hooks = jQuery.propHooks[name];
}

if (value !== undefined) {
return hooks && "set" in hooks && (ret = hooks.set(elem, value, name)) !== undefined ?
ret :
(elem[name] = value);

} else {
return hooks && "get" in hooks && (ret = hooks.get(elem, name)) !== null ?
ret :
elem[name];
}
},

propHooks: {
tabIndex: {
get: function (elem) {
// elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
// http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
// Use proper attribute retrieval(#12072)
var tabindex = jQuery.find.attr(elem, "tabindex");

return tabindex ?
parseInt(tabindex, 10) :
rfocusable.test(elem.nodeName) || rclickable.test(elem.nodeName) && elem.href ?
0 :
-1;
}
}
}
});

// Some attributes require a special call on IE
// http://msdn.microsoft.com/en-us/library/ms536429%28VS.85%29.aspx
if (!support.hrefNormalized) {
// href/src property should get the full normalized URL (#10299/#12915)
jQuery.each(["href", "src"], function (i, name) {
jQuery.propHooks[name] = {
get: function (elem) {
return elem.getAttribute(name, 4);
}
};
});
}

// Support: Safari, IE9+
// mis-reports the default selected property of an option
// Accessing the parent's selectedIndex property fixes it
if (!support.optSelected) {
jQuery.propHooks.selected = {
get: function (elem) {
var parent = elem.parentNode;

if (parent) {
parent.selectedIndex;

// Make sure that it also works with optgroups, see #5701
if (parent.parentNode) {
parent.parentNode.selectedIndex;
}
}
return null;
}
};
}

jQuery.each([
"tabIndex",
"readOnly",
"maxLength",
"cellSpacing",
"cellPadding",
"rowSpan",
"colSpan",
"useMap",
"frameBorder",
"contentEditable"
], function () {
jQuery.propFix[this.toLowerCase()] = this;
});

// IE6/7 call enctype encoding
if (!support.enctype) {
jQuery.propFix.enctype = "encoding";
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:jquery,jQuery,name,function,removeProp,elem,prop,源码,属性
From: https://blog.51cto.com/u_11837698/6081941

相关文章