【问题标题】:Using a variable in a CSS selector [duplicate]在 CSS 选择器中使用变量 [重复]
【发布时间】:2015-03-29 03:54:55
【问题描述】:

这可能是一个非常愚蠢的问题,但我有这个 CSS 选择器:

$('.result:nth-child(25) .name .title')

哪个有效。但是当我设置一个变量 a = 25 时,它会中断

$('.result:nth-child(a) .name .title')

 Uncaught Error: Syntax error, unrecognized expression: :nth-child

我做错了什么?

【问题讨论】:

  • 也许你想要的是$('.result:nth-child('+a+') .name .title')
  • 不要粗鲁,但您可以从字面上搜索您的问题标题以获取答案

标签: javascript css-selectors


【解决方案1】:

您必须在 JavaScript 中使用字符串连接手动将值插入到字符串中。试试这个:

$('.result:nth-child(' + a + ') .name .title')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-29
    • 2018-03-10
    • 2013-06-10
    • 1970-01-01
    • 2020-08-22
    • 2016-01-13
    • 1970-01-01
    • 2013-07-30
    相关资源
    最近更新 更多