jQuery:选择器(带空格的类名)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1988483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:32:03  来源:igfitidea点击:

jQuery: selector (classname with space)

jqueryclassjquery-selectors

提问by Fuxi

I'm trying to get a div which has "panel current" as classname. The problem is the space - how can I select it?

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

回答by cletus

Class names can't have spaces in them. What you have there is two classes:

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

<div class="panel current">

This div has two classes: panel and current. That's easily selected:

这个 div 有两个类:面板和当前。这很容易选择:

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

That means select all divs that have class panel andclass current.

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

回答by David Hellsing

$('div').filter(function() {
    return this.className == 'panel current';
});

OR

或者

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

Use this if you need to match an element with an exact match of class names (including spaces)

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

The other posters are right, the DiV you posted has two class names: 'panel' and 'current'; If you want to select them both, use $('.panel.current').

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

This will also include elements like:

这还将包括以下元素:

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

回答by Darin Dimitrov

panel currentis not a class name, actually it is two class names. You could use the following selector:

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

$('.panel.current')

回答by umar

The divhas two classnames:

div有两个class名字:

  • panel
  • current
  • panel
  • current

You can use $("div.panel")or $("div.current")to select it.

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