iOS手机 jQuery 为div动态绑定click事件无效
解决方法:css中添加
cursor:pointer;
span a 等标签 iOS 手机中数字自动变蓝、添加下划线并识别为手机号
解决方法:
<meta name="format-detection" content="telephone=no" />
某些时候全局属性取不到,会崩溃,如if(isLogin){};
可以使用 window.isLogin,如if(window.isLogin){},可以避免代码崩溃。
设备宽度设置 禁止屏幕放缩
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
获取地址栏url上的某个query参数
function getUrlQuery(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
定义手机端整体宽度7.5rem
// 代码需要放在body当中
<div id="node" style="width:1rem;"></div>
<script id="threadReside">
var _winWidth = document.documentElement.clientWidth || document.body.clientWidth, _style = document.getElementsByTagName("html")[0].style;
if (_winWidth == 0) {
if (window.localStorage && window.localStorage.getItem("WEBVIEW_WIN_WIDTH") != null) {
_winWidth = window.localStorage.getItem("WEBVIEW_WIN_WIDTH");
} else {
_winWidth = 320;
}
} else {
if (window.localStorage)
window.localStorage.setItem("WEBVIEW_WIN_WIDTH", _winWidth);
}
_winWidth >= 750 ? _style.fontSize = "100px" : _style.fontSize = _winWidth / 7.5 + "px";
_style.fontSize = parseFloat(_style.fontSize) * parseFloat(_style.fontSize) / document.getElementById("node").clientWidth + "px";
</script>
常用本地存储 cookie localStorage sessionStorage
document.cookie = "id=" + id + ";domain=.domain.com;path=/";
window.localStorage.setItem("WIN_WIDTH", winWidth);
window.localStorage.getItem("WIN_WIDTH")
sessionStorage.setItem("hasGo",value);
sessionStorage.getItem("hasGo");
将对象key值,首字母小写
function toLowerCaseObjectKey(jsonObj) {
if (typeof (jsonObj) == 'object') {
for (var key in jsonObj) {
let value = jsonObj[key]
if (value instanceof Array) {
for (var item of value) {
if (typeof (item) == 'object') {
toLowerCaseObjectKey(item)
}
}
}
//需要特殊处理的字符串
let condition = ['URL', 'UUID']
if (condition.includes(key)) {
jsonObj[key.toLowerCase()] = jsonObj[key];
} else {
jsonObj[key.substring(0, 1).toLowerCase() + key.substring(1)] = jsonObj[key];
}
delete (jsonObj[key]);
}
return jsonObj;
}
}
经纬度计算距离
// 方法定义 lat,lng
function getDistance( lat1, lng1, lat2, lng2){
var radLat1 = lat1*Math.PI / 180.0;
var radLat2 = lat2*Math.PI / 180.0;
var a = radLat1 - radLat2;
var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s *6378.137 ;// EARTH_RADIUS;
s = Math.round(s * 10000) / 10;
return s;
}
// 调用 return的距离单位为m
//getDistance(10.0,113.0,12.0,114.0)
判断字符串 str是否以target结尾
function confirmEnding(str, target) {
var start = str.length - target.length;
var arr = str.substr(start, target.length);
if (arr == target) {
return true;
}
return false;
}
滚动触底 加载下一页
$(window).scroll(function() {
if(($(window).scrollTop() + $(window).height() > $(document).height()-10) && !ajax){
if(maxPage>1 && page<maxPage && !ajax){
ajax=!0;//注明开始ajax加载中
page++;
if(page>=2){
$('.back-to-top').show();
}
util.getData(page)
}
}
});
获取文本行数
$('.desc').each(function(){
var styles = window.getComputedStyle(this, null);
var lh = parseInt(styles.lineHeight, 10); //行高
var h = parseInt(styles.height, 10); //控件高度
var lc = Math.round(h / lh); //文字行数
// console.log(lh+"--"+h)
// console.log(lc)
if(lc==4){
$(this).next().show()
}else{
$(this).next().hide()
}
})