Math.pow(2,50) // => 2的50次幂 js的幂运算
Math.round(.7) //=> 1.0 js的四舍五入运算
Math.ceil(.6) //=>1.0 向上求整
Math.floor(.8) //=>0 向下求整
Math.abs(-10) //=>10 求绝对值
Math.min(x,y,z) //=>返回最小值
Math.max(x,y,z) //=>返回最大值
Math.random() //=>生成一个大于等于0小于等于1.0的伪随机数
data=data.replace("]","");
<script> $(function(){ var a = 'abcdefg'; var aleng = a.length; if(aleng>5){ a.substring(0,5)+'...'; } }); </script>
CSS中最具诱惑的一个功能是能添加动画效果,除了渐变,你可以给背景色、透明度、元素大小添加动画。目前,你不能为渐变添加动画,但下面的代码可能有帮助。它通过改变背景位置,让它看起来有动画效果。
button { background-image: linear-gradient(#5187c4, #1c2f45); background-size: auto 200%; background-position: 0 100%; transition: background-position 0.5s; } button:hover { background-position: 0 0; }
<script> $(function() { $("#shouhou2").hide(); $("#shouhou3").hide(); //给div添加change事件 $("#type").change(function() { if($(this).val() == 1 ) { $("#shouhou1").show(); $("#shouhou2").hide(); $("#shouhou3").hide(); } else if($(this).val() == 2 ) { $("#shouhou2").show(); $("#shouhou1").hide(); $("#shouhou3").hide(); } else if($(this).val() == 3 ) { $("#shouhou3").show(); $("#shouhou1").hide(); $("#shouhou2").hide(); } }) }) </script> <select class="select" size="1" name="type" id="type"> <option value="1">表格</option> <option value="2">折线图</option> <option value="3">柱状图</option> </select> <div id="shouhou1" style>表格区域</div> <div id="shouhou2" style>折线图区域</div> <div id="shouhou3" style>柱状图区域</div>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <input type="text" name="alertDateQuery" id="aDate"> </body> <script type="text/javascript"> var mydateInput = document.getElementById("aDate"); var date = new Date(); var dateString = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate(); mydateInput.value = dateString; </script> </html>
当背景色不为白色的时候,要给input输入框设置成为白色,一般来说,设置color:#fff即可,但是placeholder默认的文字颜色还是灰色,这个时候需要写代码设置输入框placeholder文字颜色。
找到全局css文件,添加伪类元素,因为直接修改input颜色只作用于输入的文字,并不能改变默认字体的颜色,同时主要是要兼容火狐,IE和谷歌浏览器。
input:-moz-placeholder, textarea:-moz-placeholder { color: #fff; } input:-ms-input-placeholder, textarea:-ms-input-placeholder { color: #fff; } input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #fff ; }
页面有多个class相同的input输入框,在提交数据的时候,进行验证,验证input框不能为空,如果哪个为空,则弹出提示:
<body> 司乘卡号: <input class="personDeviceId" type="text" /><br /> 司乘卡号: <input class="personDeviceId" type="text" /><br /> 司乘卡号: <input class="personDeviceId" type="text" /><br /> <button type="button" class="btn blue" id="addBtn">保存</button> </body> <script type="text/javascript"> $("#addBtn").on("click", function() { //验证车乘 var personDeviceId = ($(".personDeviceId").length); for (var i = 0; i < personDeviceId; i++) { if ($(".personDeviceId").eq(i).val().length == 0) { alert("第" + (i + 1) + "个司乘卡号为空"); return; }; }; }) </script>
type:请求类型,GET 或 POST,默认为 GET;
async:true(异步)或 false(同步),默认情况下为true,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行;
url:发送请求的地址(跨域请求时应为绝对地址);
dataType:指定服务器返回的数据类型;
jsonpCallback:自定义JSONP回调函数名称;
success:请求成功后回调函数;
error:请求失败时调用此方法。
$(document).ready(function(){ $.ajax({ type: "get", async: false, url: "http://", dataType: "jsonp", jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback) jsonpCallback:"message",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据 success: function(json){ alert('你的名字:' + json.name + ' 年龄: ' + json.age); }, error: function(){ alert('fail'); } }); });
服务器返回的数据类型:
message({"state":"0","str":""})
通过JS判断 window.navigator.userAgent 是否包含 MicroMessenger,如果存在就是通过微信访问。
window.navigator.userAgent.indexOf("MicroMessenger") != -1
如果需要更精确怎么区分呢?比如通过朋友圈分享、好友分享。
在分享时,微信会增加自动在url后边增加参数。from=timeline代表朋友圈,from=singlemessage代表来自好友转发。所有分享的链接,还会增加isappinstalled=n。知道这个规则,就好处理了
//来自微信转发 location.search.indexOf("&isappinstalled=") != -1 //来自朋友圈 location.search.indexOf("from=timeline") != -1 //来自好友转发 location.search.indexOf("from=singlemessage") != -1