JavaScript常用技术总结!~~


如果当前窗口不是最外层窗口,把最外层窗口链接改成当前窗口

if (window != top) top.location.href = location.href;
//value值移入消失
$(":input").focus(function(){
if($(this).val()== this.defaultValue){
$(this).val(''); 
}
}).blur(function(){
if ($(this).val() == '') {
$(this).val(this.defaultValue);
}
})

 

判断ie浏览器

if(navigator.userAgent.indexOf("MSIE")>0){ 
if(navigator.userAgent.indexOf("MSIE 6.0")>0){ 
alert("ie6"); 
} 
if(navigator.userAgent.indexOf("MSIE 7.0")>0){ 
alert("ie7"); 
} 
if(navigator.userAgent.indexOf("MSIE 9.0")>0 && !window.innerWidth){//这里是重点,你懂的
alert("ie8"); 
} 
if(navigator.userAgent.indexOf("MSIE 9.0")>0){ 
alert("ie9"); 
} 
}

  

360兼容模式最高版本

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

 

判断浏览器/body/分辨率宽高度

浏览器:winHeight = document.documentElement.clientHeight;
winWidth = document.documentElement.clientWidth;
body可见区域:document.body.clientHeight
分辨率: window.screen.height

 

超简洁代码 jQuery goTop(返回顶部)

$(function() {
var $goTop = $('goTop');

$(window).scroll(function() {
if ($(this).scrollTop() != 0) {
$goTop.fadeIn();
} else {
$goTop.fadeOut();
}
});

$goTop.click(function() {
$('body, html').animate({
scrollTop: 0
}, 800);
});
});

 

获取分页列表

function getList(p) {
$.ajax({
url: '*****',
type: 'default GET (Other values: POST)',
dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
data: {param1: 'value1', limit: 10, offset: p*10},
})
.done(function(data) {
********

$('#page').tPaginator(
current: p,    // 当前页码
pageCount: Math.cell(data.total/limit),    // 总页数
callback: function(page) {
getList(page);
}    // 回调函数
);
});

 

获取单选框被选择的value的值

$('.audio-box input[name="ys-audio"]:checked').val();

 

获取地址栏参数

function GetQueryString(name) {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
         var r = window.location.search.substr(1).match(reg);
         if(r!=null)return  unescape(r[2]); return null;
    }

var account = getQueryString('account')

  

H5控制视频播放/暂停
var video = document.getElementById("Video");

$('#Video').on('play', function () {
video.play();
starUser.methods.videoHide();
}).on('pause', function () {
video.pause();
starUser.methods.videoShow();
});

 

Ajax自动转菊花

buffer : true,

 

判断元素是否显示/隐藏

if($('ele').is(':hidden')) {}  //隐藏

if($('ele').is(':visible')) {}  //显示

 

ENTER提交事件

$(document).keyup(function(event){
  if(event.keyCode ==13){
    $("#submit").trigger("click");
  }
});

 

判断是否是微信打开页面

function isWeiXin(){
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;  //是微信打开
    }else{
        return false;  //不是微信打开
    }
}

 

判断手机是Anfroid还是IOS

//判断安卓/IOS
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

 

获取页面Title

var docTitle = document.title;

 

获取地址栏地址

var href = window.location.href;

 

前端跟APP端对接方法

    function sendProjFn(source,investorBussId) { //自定义方法
        if (window.control) {
            window.control.sendProj(source,investorBussId); //sendProj APP要的方法
        }
    }

sendProjFn(1,2,3) //执行方法

 

打开APP应用和安卓应用程序

window.location.href = "myapp://tronker.com/openwith?    //IOS

window.location.href = "iOSTronkerApp://    //安卓

 

posted @ 2016-10-11 15:27  停不下的风  阅读(327)  评论(0编辑  收藏  举报