js
1.arguments:不定参
function sum()
{
arguments.lenght :根据所传参数的不同,数量改变
}
sum(1,2,4)
2.定义匿名函数 :(function sum(){
})
好处:在执行完后就会销毁
3.currentStyle.getComputedStyle 获取样式表里面的属性:currentStyle:不兼容Ie ,getComputedStyle :不兼容火狐
function getStyle(obj, attr)
{
if(obj.currentStyle)
{
return obj.currentStyle[attr];
}
else
{
return getComputedStyle(obj,false)[attr];
}
4.push:末尾添加,pop:尾部删除,shift:头部删除,unshift:头部添加
5.在js 里面
var s='23'; s[0]=chatAt(0)=2; chatAt:兼容性好
6.ajax 异步和同步
$("#insuranceWarrantyRecordExpressInfo_SendProvinceName").val("@insuranceWarrantyRecordExpressInfo.SendProvinceName").change();
在同时给一个控件赋值和执行ajax 请求,在异步情况下可能会先完成ajax ,此时将会出现问题,获取不到控件的值
同步:js本身是一个阻塞的语言,需要逐行读取代码,如果某一个程序出错,即不再执行后面所有代码;
异步:不管程序相应结果都会执行所有代码;
7.js 图片压缩
$(document).ready(function () {
function fileChange(e) {
var f = e.files[0];
var _size = f.size;
//console.log(f);
var FR = new FileReader();
FR.readAsDataURL(f);
FR.onload = function (f) {
compressImg(this.result, 1200,1200, function (data) {
//$("#newImg").attr("src",data);//保存图片压缩后的64位编码
$('#aaa').attr('src', data);
});
};
}
document.getElementById("image").addEventListener("change", function () {
fileChange(this);
}, false);
function compressImg(imgData, maxHeight,maxWidth, onCompress) {
if (!imgData) { return false };
onCompress = onCompress || function () { };
maxHeight = maxHeight || 100; //默认最大高度1000px
var canvas = document.createElement('canvas');
var img = new Image();
img.src = imgData;
img.onload = function () {
var sW = img.width, sH = img.height;
debugger
if (img.height > maxHeight || img.width > maxWidth) //将**改成c#中的或者操作符号
{
if ((img.width * maxHeight) > (img.height * maxWidth)) {
sW = maxWidth;
sH = (maxWidth * img.height) / img.width;
}
else {
sH = maxHeight;
sW = (img.width * maxHeight) / img.height;
}
}
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = sW;
canvas.height = sH;
ctx.drawImage(img, 0, 0, sW, sH);
onCompress(canvas.toDataURL("image/jpeg",70)); //压缩质量
//console.log(canvas.toDataURL("image/jpeg"));
//$('#IMAGE_URL').val(canvas.toDataURL("image/jpeg",70)) //压缩质量
};
}
});
8. 地图导航
$(function () {
let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;
let htmlHeight = document.documentElement.clientHeight || document.body.clientHeight;
//alert(htmlWidth)
let htmlDom = document.getElementsByTagName('html')[0];
htmlDom.style.fontSize = htmlWidth / 10 + 'px';
//设置腾讯地图区域初始高度
$(".mainContainer").css("height", htmlHeight + "px");
})
9.地图定位
<input type="text" />
$(document).ready(function () {
$('#search_address').click(function () {
var address = $.trim($('#address').val());
if (address != undefined && address != '') {
var url = 'http://api.map.baidu.com/geocoder/v2/?ak=eIxDStjzbtH0WtU50gqdXYCz&output=json&address=' + encodeURIComponent(address);
//根据地点名称获取经纬度信息
$.ajax({
type: "POST",
url: url,
dataType: "JSONP",
success: function (data) {
if (parseInt(data.status) == 0) {
$("#lng").html("经度:" + data.result.location.lng);
$("#lat").html("纬度:" + data.result.location.lat);
}
}
});
}
});
});