常使用的10個jQuery代碼片斷

在過去的幾年中,jQuery一直是使用最爲普遍的JavaScript腳本庫。今天咱們將爲各位Web開發者提供10個最實用的jQuery代碼片斷,有須要的開發者能夠保存起來。html

檢測Internet Explorer版本

當涉及到CSS設計時,對開發者和設計者而言Internet Explorer一直是個問題。儘管IE6的黑暗時代已通過去,IE也愈來愈不流行,它始終是一個可以容易檢測的好東西。固然了,下面的代碼也能用於檢測別的瀏覽器。瀏覽器

$(document).ready(function() {

    if (navigator.userAgent.match(/msie/i) ){

        alert('I am an old fashioned Internet Explorer');

    }

});

平穩滑動到頁面頂部

這是一個最普遍使用的jQuery效果:對一個連接點擊下會平穩地將頁面移動到頂部。這裏沒什麼新的內容,可是每一個開發者必需要會偶爾編寫一下相似函數app

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

    $("html, body").animate({ scrollTop: 0 }, "slow");

    return false;

});

固定在頂部

很是有用的代碼片斷,它容許一個元素固定在頂部。對導航按鈕、工具欄或重要信息框是超級有用的。函數

$(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')

        }

    }

用其餘內容取代html標誌

jQuery使得用另一個東西取代html標誌很簡單。能夠利用的餘地無窮無盡。工具

$('li').replaceWith(function(){

    return $("<div />").append($(this).contents());

});

檢測視窗寬度

如今移動設備比過期的電腦更廣泛,可以方便去檢測一個更小的視窗寬度會頗有幫助。幸運的是,用jQuery來作超級簡單。this

var responsive_viewport = $(window).width();

/* if is below 481px */

if (responsive_viewport < 481) {

    alert('Viewport is smaller than 481px.');

} /* end smallest screen */

自動定位並修復損壞圖片

若是你的站點比較大並且已經在線運行了好多年,你或多或少會遇到界面上某個地方有損壞的圖片。這個有用的函數可以幫助檢測損壞圖片並用你中意的圖片替換它,並會將此問題通知給訪客。spa

$('img').error(function(){

    $(this).attr('src', 'img/broken.png');

});

檢測複製、粘貼和剪切的操做

使用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!')

});

遇到外部連接自動添加target=」blank」的屬性

當連接到外部站點時,你可能使用target=」blank」的屬性去在新界面中打開站點。問題在於target=」blank」屬性並非W3C有效的屬性。讓咱們用jQuery來補救:下面這段代碼將會檢測是否連接是外鏈,若是是,會自動添加一個target=」blank」屬性。code

var root = location.protocol + '//' + location.host;

$('a').not(':contains(root)').click(function(){

    this.target = "_blank";

});

在圖片上停留時逐漸加強或減弱的透明效果

另外一個「經典的」代碼,它要放到你的工具箱裏,由於你會不時地要實現它。htm

$(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

    });

});

在文本或密碼輸入時禁止空格鍵

在不少表格領域都不須要空格鍵,例如,電子郵件,用戶名,密碼等等等。這裏是一個簡單的技巧能夠用於在選定輸入中禁止空格鍵。

$('input.nospace').keydown(function(e) {

    if (e.keyCode == 32) {

        return false;

    }

});
相關文章
相關標籤/搜索