js基础函数 & 常用方法

Javascript vue 发表时间:2019-12-13 09:36:02 作者:梁子亮 浏览次数:756

js判断移动端还是电脑pc端 1

<script type="text/javascript">
    if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
        window.location.href = "../index.html";
    } else {
        window.location.href = "../case.html";
    }
</script>

js判断移动端还是电脑pc端 2

<script>
    var is_mobi = navigator.userAgent.toLowerCase().match(/(ipod|ipad|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|wince)/i) != null;
    if (is_mobi) {
        window.location.href = "../index.html";
    }else{
        window.location.href = "../case.html";
    }
</script>

js获取浏览器get参数不支持pathinfo(仅支持?a=1&b=2形式)

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
    var context = "";
    if (r != null){
        context = r[2];
    }
    return decodeURI(context);
}

axios取消options请求

var data = { 'bar': 123 };
var options = {
    method: 'POST',
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
    data: { 'bar': 123 },
    url: BASE_URL + '/api/index/getAd',
};
axios(options).then(function(response){
    alert(123);
});
var options = {
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
};
axios.post(BASE_URL + '/api/index/getAd', {
    aaa: getQueryString('aaa'),
},options).then(function (response) {
    if(response.data.code == 20000){
        alert(123)
    }
}).catch(function (error) {
    console.log(error);
});
// 或者在common.js中重新定义一个axiosRequest方法
function axiosRequest (url,param) {
    var options = {
        headers: { 'content-type': 'application/x-www-form-urlencoded' },
    };
    return axios.post(BASE_URL + url, param,options);
}
// 调用时
axiosRequest('/api/index/getAd', {
    aaa: getQueryString('aaa'),
}).then(function (response) {
    if(response.data.code == 20000){
        
    }
}).catch(function (error) {
    console.log(error);
});

滑动小滑块效果

function swipTo (t) {
    this_ = $('.tab a').eq(t);
    var left = $(this_).width()/2 + t*$(this_).width() - $(".line-inner").width()/2;
    $('.line-inner').css({
        'transform': 'translateX(' + left + 'px)',
	'-webkit-transform': 'translateX(' + left + 'px)',
	'visibility': 'visible',
    });
};

查找@之后的非空字符串

var str = 'aa@b啊a1f';
var b = str.match(/@\S+/g)
console.log(b); // 返回的是一个数组

过滤arr1中符合条件的数据

var arr1 = [
    {
        conversationID: 12,
        tim: 35,
    },
    {
        conversationID: '123',
        tim: 678,
    },
    {
        conversationID: 123,
        tim: 890,
    },
    
];

var result = arr1.filter(item => item.conversationID == 123)
console.log(result); // 返回的是过滤后的数组

找到数组中对象符合条件的value值,删除此对象所在的key

var arr3 = [
    {
        ID: 1,
        name:'a',
    },
    {
        ID: 2,
        name:'b',
    },
    {
        ID: 3,
        name:'c',
    },
]
const index = arr3.findIndex(({ ID }) => ID === 12)
console.log(index);
if (index >= 0) {
  arr3.splice(index, 1)
}
console.log(arr3);

找出字符串'mp3'在数组['mpeg','mp3','wav']中的key位置,注意-此方法仅兼用与IE11或以上

this.accepTtype = ['mpeg','mp3','wav']
var filetype = 'mp3'
const index = this.accepTtype.findIndex(( value ) => value == filetype)
if(index == -1){
this.$message.error('上传格式不允许')
return
}
console.log(index)