【问题标题】:jquery use variables in selector not workingjquery在选择器中使用变量不起作用
【发布时间】:2013-02-08 05:55:29
【问题描述】:

当我尝试这样做时:

if (window.location.hash) {
  current_page = window.location.hash;
}
$(current_page).siblings('.page').css(display: 'none');

current_page 将是例如 #hello,所以我认为没有必要删除 #,因为我使用 ID 来识别元素。

这是我在 chrome 中的错误:
Uncaught SyntaxError: Unexpected token:
我没有得到更多。

我在这里阅读了有关如何在选择器中使用变量的其他问题,它应该可以工作。我还重写了两次以确保没有隐藏字符。
有谁知道怎么回事?

【问题讨论】:

  • window.location.hash 中有什么? -> console.log(window.location.hash)

标签: jquery variables jquery-selectors


【解决方案1】:

当然,不是:

.css(display: 'none');

但是

.css('display', 'none');

或

.css({display: 'none'});

或

.hide()

【讨论】:

  • Argh,我认为变量有问题。尽快接受!
【解决方案2】:

你在 css() 调用中的语法是错误的,使用这个:

$(current_page).siblings('.page').css('display', 'none');

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2013-10-17
    • 2010-12-23
    相关资源
    最近更新 更多