基础知识
Js预处理和各种提升
var c = 1;
function c (c) {
console.log(c);
var c = 3;
}
c(2);
//TypeError: c is not a function
预解析各种提升后,实际执行:
var c;
c = function (c) {
console.log(c);
var c = 3;
};
c = 1;
c(2);
移动端判断
const ua = navigator.userAgent.toLowerCase()
if (ua.match(/MicroMessenger/i) === "micromessenger") {}
if ( /android/i.test(ua)){}
if ( /ipad|iphone|mac/i.test(ua)){}
JQuery中attr和prop的区别
attr
和 prop
分别是单词 attribute
和 property
的缩写。在 JQuery
中, attribute
表示 HTML 文档节点的属性,property
表示 JS 对象的属性。
标签自带的属性为称为原生属性,如 checked、select、disabled
,有 true、false
两个属性。
attr(): 设置和获取原生属性。
用户点击选中表单元素,用
attr()
方法无法将其取消,不论“”、false、null
都不行;用户点击取消选中后,用attr()
方法无法将其选中。通过
attr()
给checked
赋值为false|true
, 用attr( )
获取的值为"true|false"
字符串;赋值为null
,attr()
获取值为false
布尔值;赋值为""
,attr( )
获取值为true
布尔值。prop( ): 获取值
checked、select、disabled
为true|false
布尔值。如果select
的option
定义为disabled
,则不能用val( )
获取其value
值,可用prop("value")
。不要使用
removeProp( )
来删除原生的属性。这将完全移除该属性,一旦移除,不能再次被添加到元素上,可使用prop( )
设置为false
代替。
checkbox、radio、select
元素的属性 checked
和 selected
。使用 attr()
和 prop()
取的值有差异:
<input id="chk1" type="checkbox" /> <!--不选中-->
<input id="chk2" type="checkbox" checked="checked" /> <!--选中-->
//prop()取值
$("#chk1").prop("checked") === false
$("#chk2").prop("checked") === true
//attr()取值
$("#chk1").attr("checked") === undefined
$("#chk2").attr("checked") === "checked"
最后更新于
这有帮助吗?