DIV CSS 佈局教程網

JavaScript / jQuery 常用技巧集(下):從元素替換到自訂選擇器
編輯:關於JavaScript     

接續上一篇,這裡再整理一批實用上很順手的 jQuery 小技巧。這些片段平時想不起來,需要時又特別耗時間查,建議收藏。

22. 替換元素

 $(document).ready(function() {
    $('#id').replaceWith(' <DIV>I have been replaced</DIV> ');
});

23. 延遲執行

 $(document).ready(function() {
    window.setTimeout(function() {
        // do something
    }, 1000);
});

24. 移除文字中的特定單字

 $(document).ready(function() {
    var el = $('#id');
    el.html(el.html().replace(/word/ig, ""));
});

25. 檢查元素是否存在於集合中

用 .length 判斷,這比直接檢查物件本身可靠得多:

 $(document).ready(function() {
    if ($('#id').length) {
        // do something
    }
});

26. 讓整個 DIV 可點擊

 $(document).ready(function() {
    $("div").click(function(){
        //get the url from href attribute and launch the url
        window.location=$(this).find("a").attr("href");
        return false;
    });
    // how to use <DIV><A href="index.html">home</A></DIV>
});

27. 依視窗寬度切換 class

 $(document).ready(function() {
    function checkWindowSize() {
        if ( $(window).width() > 1200 ) {
            $('body').addClass('large');
        } else {
            $('body').removeClass('large');
        }
    }
    $(window).resize(checkWindowSize);
});

28. 複製物件

 $(document).ready(function() {
    var cloned = $('#id').clone();
    // how to use <DIV id=id></DIV>
});

29. 讓元素置中於畫面

 $(document).ready(function() {
    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;
    }
    $("#id").center();
});

注意這裡把 scrollTop/scrollLeft 加了回來,否則頁面捲動後「置中」會變成「相對於視窗頂端」。

30. 寫自己的選擇器

 $(document).ready(function() {
    $.extend($.expr[':'], {
        moreThen1000px: function(a) {
            return $(a).width() > 1000;
        }
    });

    $('.box:moreThen1000px').click(function() {
        // creating a simple js alert box
        alert('The element that you have clicked is over 1000 pixels wide');
    });
});

31. 統計元素個數

 $(document).ready(function() {
    $("p").size();
});

32. 使用自訂項目符號

 $(document).ready(function() {
    $("ul").addClass("Replaced");
    $("ul > li").prepend("‒ ");

    // how to use
    // ul.Replaced { list-style : none; }
});

33. 從 Google CDN 載入 jQuery

 //Example 1
<SCRIPT src="https://www.google.com/jsapi"></SCRIPT>
<SCRIPT type=text/javascript>
    google.load("jquery", "1.2.6");
    google.setOnLoadCallback(function() {
        // do something
    });
</SCRIPT>

<SCRIPT type=text/javascript src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT>

// Example 2:(the best and fastest way)
<SCRIPT type=text/javascript src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT>

34. 全域關閉動畫效果

除錯或想確認「沒有動畫時」的版面狀態時很好用:

 $(document).ready(function() {
    jQuery.fx.off = true;
});

35. 與其他函式庫共存

 $(document).ready(function() {
    var $jq = jQuery.noConflict();
    $jq('#id').show();
});

noConflict() 把 $ 的控制權還給其他函式庫(最常見的是 Prototype),之後改用自訂的別名呼叫即可。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved