jquery有用コードフラグメント

8517 ワード





1、  Internet Explorer  
    CSS   ,          Internet Explorer      。  IE6         ,IE       ,                。   ,                。
$(document).ready(function() {
if (navigator.userAgent.match(/msie/i) ){
alert('I am an old fashioned Internet Explorer');
}
});
2、         
          jQuery  :                    。         ,                     
$("a[href='#top']").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});

	<a href="javascript:window.scrollTo(0, 0);" target="_self" class="aGoBackTop m8" id="BackTop" style="display: none;;"></a>

//    

            $(document).bind('scroll',function(e){
                var sTop = $(this).scrollTop();
                if(sTop>260){
                    $('.return-top').show();
                }else{
                    $('.return-top').hide();
                }
            });

//       
		<strong onclick="javascript:scroller('tbmov-movie', 300);">  </strong>


3、     
         ,            。     、               。
$(function(){
var $win = $(window)
var $nav = $('.mytoolbar');
var navTop = $('.mytoolbar').length && $('.mytoolbar').offset().top;
var isFixed=0;
processScroll()
$win.on('scroll', processScroll)
function processScroll() {
var i, scrollTop = $win.scrollTop()
if (scrollTop >= navTop && !isFixed) {
isFixed = 1
$nav.addClass('subnav-fixed')
} else if (scrollTop <= navTop && isFixed) {
isFixed = 0
$nav.removeClass('subnav-fixed')
}
}
4、       html  
jQuery           html     。           。
$('li').replaceWith(function(){
return $("<div />").append($(this).contents());
});
5、      
               ,                     。    , jQuery      。
var responsive_viewport = $(window).width();
/* if is below 481px */
if (responsive_viewport < 481) {
alert('Viewport is smaller than 481px.');
} /* end smallest screen */
6、           
                     ,                     。                            ,           。
$('img').error(function(){
$(this).attr('src', 'img/broken.png');
});
7、    、        
  jQuery                 、        。
$("#textA").bind('copy', function() {
$('span').text('copy behaviour detected!')
});
$("#textA").bind('paste', function() {
$('span').text('paste behaviour detected!')
});
$("#textA").bind('cut', function() {
$('span').text('cut behaviour detected!')
});
8、          target=”blank”   
         ,     target=”blank”             。    target=”blank”     W3C     。    jQuery   :                 ,   ,       target=”blank”  。
var root = location.protocol + '//' + location.host;
$('a').not(':contains(root)').click(function(){
this.target = "_blank";
});
9、                   
   “   ”  ,          ,           。
$(document).ready(function(){
$(".thumbs img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads
$(".thumbs img").hover(function(){
$(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover
},function(){
$(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout
});
});
10、              
              ,  ,    ,   ,     。                         。
$('input.nospace').keydown(function(e) {
if (e.keyCode == 32) {
return false;
}
});

DOM    
      
//                 ,
//                 。      ,
//        (:not) (:has)
//  class “selected”(.selected)    。
.filter(":not(:has(.selected))")
       
$("ul > li").click(function  {
    var index = $(this).prevAll.length;
});
      
if ($('#someDiv').length) {
//  !!!   ……
}
    
       
$("a[href='#top']").click(function {
  $("html, body").animate({ scrollTop: 0 }, "slow");
  return false;});
      
jQuery.fn.autoscroll = function(selector) {
    $('html,body').animate(
        {scrollTop: $(selector).offset.top},
        500
    };
}
//               class/area 。
$('.area_name').autoscroll;
     
           
$(function{
    var $win = $(window)
    var $nav = $('.mytoolbar');
    var navTop = $('.mytoolbar').length && $('.mytoolbar').offset.top;
    var isFixed=0;

    processScroll
    $win.on('scroll', processScroll)

    function processScroll {
    var i, scrollTop = $win.scrollTop

    if (scrollTop >= navTop && !isFixed) { 
        isFixed = 1
        $nav.addClass('subnav-fixed')
    } else if (scrollTop <= navTop && isFixed) {
        isFixed = 0
        $nav.removeClass('subnav-fixed')
    }}
       
jQuery.fn.center = function  {
    this.css('position','absolute');
    this.css('top', ( $(window).height - this.height ) / 2 +$(window).scrollTop + 'px');
    this.css('left', ( $(window).width - this.width ) / 2 +$(window).scrollLeft + 'px');
    return this;
}
//          :
$(element).center;
    
$(document).ready(function {
    $(document).mousemove(function(e){
        $(’#XY’).html(”X Axis : ” + e.pageX + ” | Y Axis ” + e.pageY);
    });
});
           
     
jQuery.preloadImages = function {
    for(var i = 0; i < arguments.length; i++) {
        $("<img />").attr('src', arguments[i]);
    }
};
//  
$.preloadImages('image1.gif', '/path/to/image2.png', 'some/image3.jpg');
         
$('img').error(function{
    $(this).attr('src', 'img/broken.png');});
           
$(document).bind('contextmenu',function(e){
    return false;
});
       
$("#someelement").on('click', function(e) {
    if( (!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1) ) {
        alert("Left Mouse Button Clicked");
    } else if(e.button == 2) {
        alert("Right Mouse Button Clicked");
    }
});
  textarea    
jQuery.fn.maxLength = function(max){
    this.each(function{
        var type = this.tagName.toLowerCase;
        var inputType = this.type? this.type.toLowerCase : null;
        if(type == "input" && inputType == "text" || inputType == "password"){

//Apply the standard maxLength
 this.maxLength = max;
        }
        else if(type == "textarea"){
 this.onkeypress = function(e){
 var ob = e || event;
 var keyCode = ob.keyCode;
 var hasSelection = document.selection? document.selection.createRange.text.length > 0 : this.selectionStart != this.selectionEnd;
 return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
 };
 this.onkeyup = function{
 if(this.value.length > max){
 this.value = this.value.substring(0,max);
 }
 };
        }
    });
};
//  
$('#mytextarea').maxLength(500);
          
$('input.nospace').keydown(function(e) {
    if (e.keyCode == 32) {
        return false;
    }});