jQuery.attr()是否保证小写?
如果找到属性,jQuery.attr()是否保证小写?,jquery,attributes,Jquery,Attributes,如果找到属性,$(选择器).attr(名称)是否保证结果为小写?它将在任何情况下返回设置的值 <div class="sOmEcLaSs">content</div> 如果要转换为小写,可以使用.toLowerCase() jQuery的attr返回语句的代码(不是Sizzle): 或 否,因为.attr调用javascript.getAttribute方法时没有任何参数。 正如您在下面的代码中所看到的 getAttribute默认值为0,不区分大小写,因此它会准确
$(选择器).attr(名称)
是否保证结果为小写?它将在任何情况下返回设置的值
<div class="sOmEcLaSs">content</div>
如果要转换为小写,可以使用.toLowerCase()
jQuery的
attr
返回语句的代码(不是Sizzle):
或
否,因为.attr调用javascript.getAttribute方法时没有任何参数。 正如您在下面的代码中所看到的 getAttribute默认值为0,不区分大小写,因此它会准确返回找到的内容
ATTR: function(elem, match){
var name = match[1],
result = Expr.attrHandle[ name ] ?
Expr.attrHandle[ name ]( elem ) :
elem[ name ] != null ?
elem[ name ] :
elem.getAttribute( name ),
value = result + "",
type = match[2],
check = match[4];
return result == null ?
type === "!=" :
type === "=" ?
value === check :
type === "*=" ?
value.indexOf(check) >= 0 :
type === "~=" ?
(" " + value + " ").indexOf(check) >= 0 :
!check ?
value && result !== false :
type === "!=" ?
value !== check :
type === "^=" ?
value.indexOf(check) === 0 :
type === "$=" ?
value.substr(value.length - check.length) === check :
type === "|=" ?
value === check || value.substr(0, check.length + 1) === check + "-" :
false;
},
jQuery不能依赖区分大小写的属性搜索,而且仍然是跨浏览器兼容的。在旧的IE DOM中,我记得所有标记和属性都以大写形式存储和返回;因此标记
在内部呈现为
。因此,在Netscape或Firefox中,属性名应该是id
,在IE中应该是id
。但是,即使使用动态创建的元素(以所需的大小写存储),IE内部也存在不一致。例如,IE6和IE8在getAttribute()
中的行为完全不同。比较:
<div></div>
var myDiv = document.getElementsByTagName('div')[0];
myDiv.setAttribute('id','id1');
myDiv.setAttribute('ID','id2');
console.log(x.getAttribute('ID')); // IE6, return "id1", IE8, returns "id2"
console.log(x.getAttribute('ID',true)); // IE6, return "id2", returns "id2"
var myDiv=document.getElementsByTagName('div')[0];
myDiv.setAttribute('id','id1');
myDiv.setAttribute('ID','id2');
console.log(x.getAttribute('ID'));//IE6,返回“id1”,IE8,返回“id2”
console.log(x.getAttribute('ID',true));//IE6,返回“id2”,返回“id2”
你能保证用户不会禁用javascript吗
ATTR: function(elem, match){
var name = match[1],
result = Expr.attrHandle[ name ] ?
Expr.attrHandle[ name ]( elem ) :
elem[ name ] != null ?
elem[ name ] :
elem.getAttribute( name ),
value = result + "",
type = match[2],
check = match[4];
return result == null ?
type === "!=" :
type === "=" ?
value === check :
type === "*=" ?
value.indexOf(check) >= 0 :
type === "~=" ?
(" " + value + " ").indexOf(check) >= 0 :
!check ?
value && result !== false :
type === "!=" ?
value !== check :
type === "^=" ?
value.indexOf(check) === 0 :
type === "$=" ?
value.substr(value.length - check.length) === check :
type === "|=" ?
value === check || value.substr(0, check.length + 1) === check + "-" :
false;
},
<div></div>
var myDiv = document.getElementsByTagName('div')[0];
myDiv.setAttribute('id','id1');
myDiv.setAttribute('ID','id2');
console.log(x.getAttribute('ID')); // IE6, return "id1", IE8, returns "id2"
console.log(x.getAttribute('ID',true)); // IE6, return "id2", returns "id2"