js/jquery 备忘录

 iframe下子页获取父页面元素方法:格式:宋体 小字体

           1:   $(window.parent.document).find("#panels4").css("width","100%");//产生水平滚动条  #panels4:父页面的元素

==============||format日期||===========================

var  nowdate= new Date();
var da=nowdate.format("yyyy-MM-dd");


  //格式日期方法体
  Date.prototype.format = function(format) //author: meizz
  {
  var o = {
  "M+" : this.getMonth()+1, //month
  "d+" : this.getDate(), //day
  "h+" : this.getHours(), //hour
  "m+" : this.getMinutes(), //minute
  "s+" : this.getSeconds(), //second
  "q+" : Math.floor((this.getMonth()+3)/3), //quarter
  "S" : this.getMilliseconds() //millisecond
  }
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
  (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  for(var k in o)if(new RegExp("("+ k +")").test(format))
  format = format.replace(RegExp.$1,
  RegExp.$1.length==1 ? o[k] :
  ("00"+ o[k]).substr((""+ o[k]).length));
  return format;
  } 


=========||php与java中时间加天数的问题||======

<?php

$date = '2009-12-25';

echo date('Y-m-d',strtotime($date . '+365 day'));

echo date('Y-m-d',strtotime($date . '+12 month'));

echo date('Y-m-d',strtotime($date . '+1 year'));

?>

得到 2010-12-25

$date = '2009-12-25';
$newdate = strtotime($date) + 3600 * 24 * 365;
$newdate = date('y-m-d',$newdate)

得到 2010-12-25

java中天数加1

法一:

String d="2004-01-01"; 
DateFormat format=new SimpleDateFormat("yyyy-MM-dd"); 
Date dd=format.parse(d); 
Calendar calendar=Calendar.getInstance(); 
calendar.setTime(dd); 
calendar.add(Calendar.DAY_OF_MONTH,1); 
System.out.println(format.format(calendar.getTime()));

法二:

//Parse your String to Date 
Date date = ...... 
Calendar cal = Calendar.getInstance(); 
cal.setTime(date); 
cal.add(Calendar.DATE, 1); 
Date newDate = cal.getTime();

//Parse the newDate to String 


  ================||js加一天减一天||============

  //加减天数方法体
function addDate(dd,dadd){  
var a = new Date(dd)  
a = a.valueOf()  
a = a + dadd * 24 * 60 * 60 * 1000  
a = new Date(a)  
return a;  
}  
  
  //获取前一天 日志
  function beforlog(){

 var  nowdat=$("#currentDate").attr("value")
 var lnowday=nowdat.split("-");
 var year=lnowday[0];
 var month=lnowday[1];
 var day=lnowday[2];
  yday = addDate(year+"/"+month+"/"+day,-1);  
 var da=yday.format("yyyy-MM-dd");
 $("#currentDate").attr("value",da);
 }
//获取后一天日志
  function afterlog(){
 var  nowdat=$("#currentDate").attr("value")
 var lnowday=nowdat.split("-");
 var year=lnowday[0];
 var month=lnowday[1];
 var day=lnowday[2];
  yday = addDate(year+"/"+month+"/"+day,1);  
 var da=yday.format("yyyy-MM-dd");
 $("#currentDate").attr("value",da);
 }



          =========||解析JSON||=========

$.ajax({
    type: 'POST',
    async:false,
    data: "bs_Equip="+bs_Equip,
       url:'${pageContext.request.contextPath}/control/Date_manage/NowDate_manage!tallyWithTimes.action',
       success: function(doc) {
        var json=eval("("+doc+")");   //解析JSON 有时候要注意的地方
       }
   });

    ==================||判断IE是否开啦检测模式||================================

<script language="javascript" type="text/javascript">
   var version = navigator.appVersion;
   var start = version.indexOf("MSIE");
   var temp = version.slice(start+5, start+6);
   if(temp=="9"){
      alert(9);
   }else if(temp=="8"){//IE8 非兼容模式
      alert(8);
   }else if(temp=="7"){//IE8 兼容模式margin-left: 200px;
      alert(7);
   }else if(temp=="6"){
      alert(6);
   }else if(temp=="5"){
      alert(5);
   }
</script>
在IE8下,如果开启兼容性视图,返回值是7,否则返回8

分享10个超实用的jQuery代码片段

jQuery以其强大的功能和简单的使用成为了前端开发者最喜欢的JS类库,在这里我们分享一组实用的jQuery代码片段,希望大家喜欢!

jQuery平滑回到顶端效果

$(document).ready(function() {

	$("a.topLink").click(function() {
		$("html, body").animate({
			scrollTop: $($(this).attr("href")).offset().top + "px"
		}, {
			duration: 500,
			easing: "swing"
		});
		return false;
	});

});

运行代码:

20864b59-a995-4318-a242-b3038f83f2c3

jQuery处理图片尺寸

$(window).bind("load", function() {
	// 图片修改大小
	$('#imglist img').each(function() {
		var maxWidth = 120;
		var maxHeight = 120;
		var ratio = 0;
		var width = $(this).width();
		var height = $(this).height();
	
		if(width > maxWidth){
			ratio = maxWidth / width;
			$(this).css("width", maxWidth);
			$(this).css("height", height * ratio);
			height = height * ratio;
		}
      
		if(height > maxHeight){
			ratio = maxHeight / height;
			$(this).css("height", maxHeight);
			$(this).css("width", width * ratio);
			width = width * ratio;
		}
	});

});

运行代码:

5a2271a5-f363-4b34-8d2f-f0ad03121ced

jQuery实现的滚动自动加载代码

var loading = false;
$(window).scroll(function(){
	if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){
		if(loading == false){
			loading = true;
			$('#loadingbar').css("display","block");
			$.get("load.php?start="+$('#loaded_max').val(), function(loaded){
				$('body').append(loaded);
				$('#loaded_max').val(parseInt($('#loaded_max').val())+50);
				$('#loadingbar').css("display","none");
				loading = false;
			});
		}
	}
});

$(document).ready(function() {
	$('#loaded_max').val(50);
});

jQuery测试密码强度

$('#pass').keyup(function(e) {
     var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
     var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
     var enoughRegex = new RegExp("(?=.{6,}).*", "g");
     if (false == enoughRegex.test($(this).val())) {
             $('#passstrength').html('More Characters');
     } else if (strongRegex.test($(this).val())) {
             $('#passstrength').className = 'ok';
             $('#passstrength').html('Strong!');
     } else if (mediumRegex.test($(this).val())) {
             $('#passstrength').className = 'alert';
             $('#passstrength').html('Medium!');
     } else {
             $('#passstrength').className = 'error';
             $('#passstrength').html('Weak!');
     }
     return true;
});

运行代码:

5ae6bca8-b12b-48b8-861a-8d9ab09e6641

jQuery实现的DIv高度保持一致

var maxHeight = 0;

$("div").each(function(){
   if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});

$("div").height(maxHeight);

运行代码:

ac7c5175-9282-41c0-a766-c56707946c7b

简单处理IE6上PNG格式文件

$('.pngfix').each( function() {
   $(this).attr('writing-mode', 'tb-rl');
   $(this).css('background-image', 'none');
   $(this).css( 'filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="path/to/image.png",sizingMethod="scale")');
});

将class=pngfix添加到需要处理的对象即可。

jQuery处理JSON

function parseJson(){
	//Start by calling the json object, I will be using a 
        //file from my own site for the tutorial 
	//Then we declare a callback function to process the data
	$.getJSON('hcj.json',getPosts);
 
	//The process function, I am going to get the title, 
        //url and excerpt for 5 latest posts
	function getPosts(data){
 
		//Start a for loop with a limit of 5 
		for(var i = 0; i < 5; i++){
			//Build a template string of 
                        //the post title, url and excerpt
			var strPost = '<h2>' 
				      + data.posts[i].title
				      + '</h2>'
				      + '<p>'
				      + data.posts[i].excerpt
				      + '</p>'
				      + '<a href="'
				      + data.posts[i].url
				      + '" title="Read '
				      + data.posts[i].title
				      + '">Read ></a>';
 
			//Append the body with the string
			$('body').append(strPost);
 
		}
	}
 
}
 
//Fire off the function in your document ready
$(document).ready(function(){
	parseJson();				   
});

 

jQuery实现让整个div可以被点击

$(".myBox").click(function(){
     window.location=$(this).find("a").attr("href"); 
     return false;
});

运行代码:

4bfb3dce-1681-470c-b47a-7490df12f3b0

jQuery实现的Facebook风格的图片预加载效果

var nextimage = "http://www.gbtags.com/gb/networks/uploadimgthumb/55d67b14-fb25-45e7-acc8-211a41047ef0.jpg";
$(document).ready(function(){
  window.setTimeout(function(){
    var img = $("<img>").attr("src", nextimage).load(function(){
     $('div').append(img);
    });
  }, 100);
});

运行代码:

b1a87e30-e33f-4369-92fc-55e8fd628816

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值