簡體   English   中英

$(':target')在Internet Explorer 11中返回長度0

[英]$(':target') returns length 0 in Internet Explorer 11

我正在嘗試補償網頁中的錨點移動位置,因此它沒有被固定的標題覆蓋。

我解決了這個anwser的問題,它適用於除IE以外的每個瀏覽器。 它只是在錨點移動后向上滾動幾個像素。

代碼如下:

(function($, window) {
    var adjustAnchor = function() {

        var $anchor = $(':target'),
                    fixedElementHeight = 160;
        console.log("Anchor: "+ JSON.stringify($anchor));

        if ($anchor.length > 0) {
            $('html, body')
                .stop()
                .animate({
                    scrollTop: $anchor.offset().top - fixedElementHeight
                }, 200);
        }
    };

    $(window).on('hashchange load', function() {
        adjustAnchor();
    });

})(jQuery, window);

現在,IE11中console.log的輸出是這樣的:

{
    "length": 0,
    "prevObject": {
        "0": {
            "__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR": false,
            "_html5shiv": 1,
            "jQuery1111049273906621767055": 4
        },
        "context": {
            "__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR": false,
            "_html5shiv": 1,
            "jQuery1111049273906621767055": 4
        },
        "length": 1
    },
    "context": {
        "__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR": false,
        "_html5shiv": 1,
        "jQuery1111049273906621767055": 4
    },
    "selector": ":target"
}

問題顯然是“長度”:0,這意味着沒有選擇任何東西。 為什么是這樣? :目標選擇器應該在IE11中正常工作,但jQuery沒有抓住它。

請原諒我對jQuery的無知以及我糟糕的英語。

我通過將名稱屬性旁邊的id屬性添加到錨點的目標來解決了這個問題,如下所示:

<a class="anchor" name="condiciones" id="condiciones"></a> 

由於某種原因,name屬性足以讓錨點在每個瀏覽器中工作,但為了在Internet Explorer中使用jQuery獲取它們,必須使用id屬性創建錨點。

我討厭IE。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM