条件为 jquery 的选择器开关 ID
A selector switch id with jquery as condition
也许我问的太简单了,也许不是。无论如何,我需要在按下 link 时调用页面的某个锚点,它会进入一个特定的 URL 并使用特定的锚点向下滚动到该锚点。
我正在用 jquery 这样做:
$(document).ready(function() {
var anchor = window.location.hash.substring(1);
if(anchor!="") {
var newAnchor= "'#anchor-" +anchor+"'";
$(newAnchor).trigger("click");
}
});
我认为这是不可能的,我肯定没见过,我得到以下错误:
Uncaught Error: Syntax error, unrecognized expression: '#anchor-2015'
是否可以像这样动态调用选择器?还有别的办法吗?
谢谢。
删除单引号,不需要它们。
var newAnchor= "#ancla-" + anchor;
是的,这是可能的。你打错引号了。
$(document).ready(function() {
var anchor = window.location.hash.substring(1);
if(anchor!=""){
var newAnchor= "#anchor-" + anchor;
$(newAnchor).trigger("click");
}
});
也许我问的太简单了,也许不是。无论如何,我需要在按下 link 时调用页面的某个锚点,它会进入一个特定的 URL 并使用特定的锚点向下滚动到该锚点。
我正在用 jquery 这样做:
$(document).ready(function() {
var anchor = window.location.hash.substring(1);
if(anchor!="") {
var newAnchor= "'#anchor-" +anchor+"'";
$(newAnchor).trigger("click");
}
});
我认为这是不可能的,我肯定没见过,我得到以下错误:
Uncaught Error: Syntax error, unrecognized expression: '#anchor-2015'
是否可以像这样动态调用选择器?还有别的办法吗?
谢谢。
删除单引号,不需要它们。
var newAnchor= "#ancla-" + anchor;
是的,这是可能的。你打错引号了。
$(document).ready(function() {
var anchor = window.location.hash.substring(1);
if(anchor!=""){
var newAnchor= "#anchor-" + anchor;
$(newAnchor).trigger("click");
}
});