【www.gdgbn.com--jquery】

jquery 返回顶部、图片比例缩小、验证元素是否存在
返回顶部代码

$(document).ready(function() {

  $("a[href*=#]").click(function() {

   if (location.pathname.replace(/^//,"") == this.pathname.replace(/^//,"")

       && location.hostname == this.hostname) {

     var $target = $(this.hash);

     $target = $target.length && $target || $("[name=" + this.hash.slice(1) +"]");

     if ($target.length) {

       var targetoffset = $target.offset().top;

       $("html,body").animate( { scrolltop: targetoffset }, 900);

       return false;

     }

   }

  });

});

图片比例缩小

代码如下:

 

$(document).ready(function(){
var ywidth = 450;//初始最大宽度
$(”img”).each(function(){
   if($(this).width() > ywidth) {
    $(this).width(ywidth);
    $(this).mouseo教程ver(function(){
     $(this).css教程(”cursor”,”hand”);
    });
    $(this).click(function(){
     window.location.href = $(this).attr(”src”);
    });
   }
});
});

jquery 验证元素是否存在

 

$(document).ready(function() {

  if ($("#id").length) {

    // do something

  }

});

本文来源:http://www.gdgbn.com/wangyezhizuo/29919/