Javascript $(';:target';)在Internet Explorer 11中返回长度0
我试图补偿网页中锚的移动位置,使其不被固定的标题覆盖 我解决了这个问题,它适用于除IE之外的所有浏览器。它只是在锚移动后向上滚动几个像素 代码如下:Javascript $(';:target';)在Internet Explorer 11中返回长度0,javascript,jquery,internet-explorer-11,Javascript,Jquery,Internet Explorer 11,我试图补偿网页中锚的移动位置,使其不被固定的标题覆盖 我解决了这个问题,它适用于除IE之外的所有浏览器。它只是在锚移动后向上滚动几个像素 代码如下: (function($, window) { var adjustAnchor = function() { var $anchor = $(':target'), fixedElementHeight = 160; console.log("Anchor: "+ J
(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,这意味着没有选择任何内容。为什么会这样?:target选择器在IE11中应该可以很好地工作,但是jQuery没有抓住它
请原谅我对jQuery的无知和我糟糕的英语。我解决了这个问题,在锚的目标的名称属性旁边添加了id属性,如下所示:
<a class="anchor" name="condiciones" id="condiciones"></a>
出于某种原因,name属性足以使锚定在每个浏览器中工作,但为了在Internet Explorer中使用jQuery获取锚定,锚定必须使用id属性
我讨厌IE。你能把
var$anchor=$(':target'),
改成var$anchor=$(window.location.hash),
?@epascarello那么向上滚动就不起作用了,因为$anchor没有长度属性。这就是$anchor现在拥有的{“上下文”:{“jquery1110726621762832618”:4},“选择器”:“#条件”}