最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

jQuery tips and tricks

標(biāo)簽:
Html/CSS JavaScript

1. How to judge element is visible ?

    if($(".nav").is(":visible")) {
        // This element is visible
    }

This filter is very power, for example:

    <div >
    <div id="test">This element is hidden.
    </div>
    if ($("#test").is(":hidden")) {
        alert("this element is hidden"); // execute
    }

Inside the jQuery, it use offsetWidth and offsetHeight to judge whether an element
is visible, not through CSS properties visibility or display:

    Sizzle.selectors.filters.visible = function(elem){
	    return elem.offsetWidth > 0 || elem.offsetHeight > 0;
    };

2. Scroll to an element

    // This example doesn't work properly
    var eleTop = $("#p2").offset().top;
    $("body").scrollTop(eleTop);

OK, it’s a trick that we must set selecter to “html, body”:

    // Works well
    var eleTop = $("#p2").offset().top;
    $("html, body").scrollTop(eleTop);

Add some animations:

    var eleTop = $("#p2").offset().top;
    $("html, body").animate({
        "scrollTop": eleTop
    }, "slow");

3. Another way to find the index of an element within a set

In the previous
article, I use the jQuery index method to achieve this.

Below are some other ways:

    <ul class="menu">
        <li>menu 1</li>
        <li>menu 2</li>
        <li>menu 3</li>
    </ul>
    // Previous method
    var lis = $("ul.menu li").click(function() {
        lis.index(this); // "menu 3" -> 2
    });
    // Method 1
    var lis = $("ul.menu li").click(function() {
        alert($(this).prevAll().length); // "menu 3" -> 2
    });
    // Method 2
    var lis = $("ul.menu li").click(function() {
        alert(lis.length - $(this).nextAll().length - 1);
    });
    // Method 3
    var lis = $("ul.menu li").click(function() {
        alert($.inArray(this, lis));
    });

4. Loop in a graceful way - $.each

We may loop through object or array like this:

    var obj = {
        "name": "zhangsan",
        "sex": "man",
        "age": 21
    };
    for (var i in obj) {
        console.log("key:" + i + " value:" + obj[i]);
    } 

    var arr = ["zhangsan", "lisi", "wanger"];
    for (var i = 0; i < arr.length; i++) {
        console.log("index:" + i + " value:" + arr[i]);
    }

Now, with jQuery:

    // key:name value:zhangsan
    // key:sex value:man
    // key:age value:21
    $.each(obj, function(i, item) {
        console.log("key:" + i + " value:" + item);
    }); 

    // index:0 value:zhangsan
    // index:1 value:lisi
    // index:2 value:wanger
    $.each(arr, function(i, item) {
        console.log("index:" + i + " value:" + item);
    });

4. Return false to prevent default and stop propagation

    <div id="link2">
        <a >Go to Google</a>
    </div>
    $("#link1").click(function(event) {
        alert("click link1.");
    });
    // 1. Only prevent default action of hyperlink, the alert dialog is showing.
    $("#link1 a").click(function(event) {
        event.preventDefault();
    });
    // 2. No alert dialog shows
    $("#link1 a").click(function(event) {
        event.preventDefault();
        event.stopPropagation();
    });
    // 3. This is the same as previous code
    $("#link1 a").click(function(event) {
        return false;
    });
點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消