Javascript 对'的行为;rel';属性:prop()与attr()的比较
有人能解释一下jQuery中的这种(奇怪的)行为吗?这是小提琴: 问题是,为什么jQuery在调用Javascript 对'的行为;rel';属性:prop()与attr()的比较,javascript,jquery,html,Javascript,Jquery,Html,有人能解释一下jQuery中的这种(奇怪的)行为吗?这是小提琴: 问题是,为什么jQuery在调用.prop()方法时不将rel设置为tooltip,而.attr()工作正常 此外,当设置类型时,这两种方法都有效。 我唯一的猜测是.prop()执行一些验证?在将jQuery 1.6用于HTML属性和对象属性之前,不允许将rel与按钮一起使用 在之后,jQuery 1.6attr()仅用于HTML属性,如href或rel,类或任何其他属性 prop()现在用于对象属性,如 var Obj = {
.prop()
方法时不将rel
设置为tooltip
,而.attr()
工作正常
此外,当设置类型时,这两种方法都有效。
我唯一的猜测是.prop()
执行一些验证?在将jQuery 1.6用于HTML属性和对象属性之前,不允许将rel
与按钮一起使用
在之后,jQuery 1.6attr()
仅用于HTML属性,如href
或rel
,类
或任何其他属性
prop()
现在用于对象属性,如
var Obj = {
propOne: 'somevalue'
}
或
从:
。。。.prop()文件
方法提供了一种显式检索属性值的方法,而
.attr()检索属性
使用prop()
的关键短语是:
属性没有相应的(HTML)属性,只是属性。
文档中的示例:
elem.checked
// true (Boolean) Will change with checkbox state
$(elem).prop( 'checked')
// true (Boolean) Will change with checkbox state
elem.getAttribute('checked')
// "checked" (String) Initial state of the checkbox;
// does not change
$(elem).attr('checked') (1.6)
// "checked" (String) Initial state of the checkbox;
// does not change
$(elem).attr('checked') (1.6.1+)
// "checked" (String) Will change with checkbox state
$(elem).attr('checked') (pre-1.6)
// true (Boolean) Changed with checkbox state
elem.checked
// true (Boolean) Will change with checkbox state
$(elem).prop( 'checked')
// true (Boolean) Will change with checkbox state
elem.getAttribute('checked')
// "checked" (String) Initial state of the checkbox;
// does not change
$(elem).attr('checked') (1.6)
// "checked" (String) Initial state of the checkbox;
// does not change
$(elem).attr('checked') (1.6.1+)
// "checked" (String) Will change with checkbox state
$(elem).attr('checked') (pre-1.6)
// true (Boolean) Changed with checkbox state