Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对'的行为;rel';属性:prop()与attr()的比较_Javascript_Jquery_Html - Fatal编程技术网

Javascript 对'的行为;rel';属性:prop()与attr()的比较

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 = {

有人能解释一下jQuery中的这种(奇怪的)行为吗?这是小提琴:

问题是,为什么jQuery在调用
.prop()
方法时不将
rel
设置为
tooltip
,而
.attr()
工作正常

此外,当设置
类型时,这两种方法都有效。

我唯一的猜测是
.prop()
执行一些验证?在将jQuery 1.6用于HTML属性和对象属性之前,不允许将
rel
按钮一起使用

之后,jQuery 1.6
attr()
仅用于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