【问题标题】:jQuery: selector (classname with space)jQuery:选择器(带空格的类名)
【发布时间】:2010-12-31 14:27:51
【问题描述】:

我正在尝试获取一个将“面板当前”作为类名的 div。 问题是空间 - 我该如何选择它?

【问题讨论】:

标签: jquery class jquery-selectors


【解决方案1】:

类名中不能有空格。你有两个类:

<div class="panel current">

这个 div 有两个类:panel 和 current。这很容易选择:

$("div.panel.current")...

这意味着选择所有具有类面板当前类的div。

【讨论】:

    【解决方案2】:
    $('div').filter(function() {
        return this.className == 'panel current';
    });
    

    $("div[class='panel current']");
    

    如果您需要将元素与类名完全匹配(包括空格)匹配,请使用此选项

    其他发帖人是对的,你贴的DiV有两个类名:'panel'和'current'; 如果您想同时选择它们,请使用$('.panel.current')

    这还将包括以下元素:

    <div class="foo panel bar current"></div>
    

    【讨论】:

      【解决方案3】:

      panel current 不是类名,实际上是两个类名。您可以使用以下选择器:

      $('.panel.current')
      

      【讨论】:

        【解决方案4】:

        div 有两个 class 名称:

        • panel
        • current

        您可以使用$("div.panel")$("div.current") 来选择它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-04
          • 2015-02-26
          • 1970-01-01
          • 2017-03-05
          • 2016-07-16
          • 2010-11-16
          • 2018-06-27
          相关资源
          最近更新 更多