Javascript备忘
js输出对象类型:
Object.prototype.toString.apply(s)
设置单行点击效果:
obj.style.background = "#efefef";
setTimeout(function() {
obj.style.background = "";}, 100);
js编码:
alert(encodeURIComponent('你'));
js解码:
var a= encodeURIComponent('你');
alert(decodeURI(a));
//自执行
(function (){
aler('hello');
}
}());
//页面输出:
document.write();
//改变背景色(jquery)
$("#ajsj li").css("background-color","white");
obj.style.background = "#efefef";
//改变div样式(jquery)
jQuery("#quanwen").css("display","block");
//onclick 执行href:
onclick="window.location.href='javascript:history.go(-1)'"
location.reload(true);
//加载完执行:
window.onload=function(){}
window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行。 window.onload不能同时编写多个,如果有多个window.onload方法,只会执行一个
$(document).ready(function(){})可以简写成$(function(){});
$(document).ready()是DOM结构绘制完毕后就执行,不必等到加载完毕。 $(document).ready()可以同时编写多个,并且都可以得到执行
//解决ie不兼容getElementsByName 问题
var getElementsByName = function(tag, name){
var returns = document.getElementsByName(name);
if(returns.length > 0) return returns;
returns = new Array();
var e = document.getElementsByTagName(tag);
for(var i = 0; i < e.length; i++){
if(e[i].getAttribute("name") == name){
returns[returns.length] = e[i];
}
}
return returns;
}
//解决js代码中加入alert()就成功执行,不加就不对
setTimeout(aa(),1000)
//窗口只能弹出一个
var child;
function creditStatus(){
if (child==null||child.closed == true) {
child = middleOpen("sellRegisterSaleListAction.do?method=creditStatus","","scrollbars=yes,width=900,height=700,left=50,top=10,menubar=no,toolbar=no,resizable=yes,scrolling=yes,status=yes");
}else{
alert("<bean:message key='WebContent.newsis.repair.repairList.mes.opened.page'/>");
child.focus();
}
}
function middleOpen(myUrl,myName,myStyle){
var clientScreenHeight = screen.height;
var clientScreenWidth = screen.width;
var heightStart = myStyle.indexOf("height=");
var hetghtEnd = 0;
for(var i=heightStart;i<=myStyle.length;i++){
if((myStyle.charAt(i) == ',') || (i==myStyle.length)){//遇到“,”退出,表示结束,或已经到达字符串末尾
hetghtEnd = i;
break;
}
}
var myHeight = myStyle.substring(heightStart+7,hetghtEnd);
var widthiStart = myStyle.indexOf("width=");
var widthEnd = 0;
for(var i=widthiStart;i<=myStyle.length;i++){
if((myStyle.charAt(i) == ',') || (i==myStyle.length)){//遇到“,”退出,表示结束,或已经到达字符串末尾
widthEnd = i;
break;
}
}
var myWidth = myStyle.substring(widthiStart+6,widthEnd);
var openWindowHeight = parseInt(myHeight);//提取高度
var openWindowWidth = parseInt(myWidth);//提取宽度
if(isNaN(openWindowHeight) || isNaN(openWindowWidth)){//如果提取的值不正确,退出,提示!
alert("Can't open,please contact the system admin!");
return;
}
var popTop = (clientScreenHeight-openWindowHeight)/2;
var popleft = (clientScreenWidth-openWindowWidth)/2;
var result = window.open(myUrl,myName,myStyle+",top="+popTop+",left="+popleft);
result.focus();
arrchild[nowIndex++]=result;
return result;
}
//ie兼容用
var getElementsByName = function(tag, name){
var returns = document.getElementsByName(name);
if(returns.length > 0) return returns;
returns = new Array();
var e = document.getElementsByTagName(tag);
for(var i = 0; i < e.length; i++){
if(e[i].getAttribute("name") == name){
returns[returns.length] = e[i];
}
}
return returns;
}
//清除用
function clearAll(){
document.forms[0].reset();
document.getElementById("r_vendorCode").value="";
document.getElementById("r_vendorName").value="";
document.getElementById("r_contactPerson").value="";
document.getElementById("r_vendorAddress").value="";
document.getElementById("r_vendorPhoneNo").value="";
var cce=getElementsByName("select","r_paymentMethod")[0][0];
cce.setAttribute("selected","selected");
var t=getElementsByName("select","r_strProvince")[0][0];
t.setAttribute("selected","selected");
var city=getElementsByName("select","r_strCityId")[0][0];
city.setAttribute("selected","selected");
}
function trim(str){ //删除左右两端的空格
return str.replace(/(^\s*)|(\s*$)/g, "");
}
function ltrim(str){ //删除左边的空格
return str.replace(/(^\s*)/g,"");
}
function rtrim(str){ //删除右边的空格
return str.replace(/(\s*$)/g,"");
}
//判断javascript类型
typeof aa
//转换为int
parseInt(aa);
//javaScript追加
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.onload = script.onreadystatechange = function() {
if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete" ) {
help();
script.onload = script.onreadystatechange = null;
} };
script.src= 'http://libs.baidu.com/jquery/1.9.0/jquery.js';
head.appendChild(script);
//图片预览
<img id="imghead" src="${head}"/><input name="image" onchange="previewImage(this)" type="file" class="photo_put" value="" />
function previewImage(file)
{
var MAXWIDTH = 300;
var MAXHEIGHT = 300;
if (file.files && file.files[0])
{
var img = document.getElementById('imghead');
var reader = new FileReader();
reader.onload = function(evt){img.src = evt.target.result;}
reader.readAsDataURL(file.files[0]);
}
}
$("#orders > header span").bind("click",function(e){
var projectType= $("a",e.currentTarget).attr("value");
alert(projectType);
$('#ordersFactProject').val(projectType);
$("#step1Form").submit();
});
$('#test')[0].src //获取src
//jquery解析json
JSON.stringify();
var p_class = $("p").attr("class");
$("ul",this).removeAttr("style");//移除样式 选择这个元素下的ul
$(this).parent().attr('class','active');//更换样式
$(this).parent().parent().html();
$(".icon-box").next().removeClass();//同级下一个元素
$(".icon-box").prev().removeClass();//同级上一个元素
$("p:eq(1)") //选择第二个 <p> 元素
判断为正整数:
if(value <0 || value!=parseInt(value) ){
alert("不是正整数");
}else{
alert("是正整数");
}