如何选择具有特定类

如何选择具有特定类

本文介绍了JQuery选择器:如何选择具有特定类*和* id的项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网页上选择具有特定类和ID的HTML元素。以下是标记:

I'm trying to select an HTML element on my page that has a specific class and ID. Here's the tag:

<div class="statusLight" id="green"></div>

我试过没有运气:

$statusLight = $('.statusLight#green');

我知道我可以简单说

$statusLight = $('#green');

但我试图找到一种基于类来选择它的方法。

But I was trying to find a way to select it based on its class as well. Any help would be appreciated.

推荐答案

#green.statusLight .statusLight#green 是有效的选择器,应该选择您要查找的元素。

Both #green.statusLight and .statusLight#green are valid selectors and should select element you're looking for. First one will be faster though.

您在文档加载后使用 $(...)即从 $(document).ready(function(){...})或通过将脚本放在元素之后?

Are you using $(...) after your document is loaded, i.e. from $(document).ready(function() { ... }) or by placing your script after your element?

这篇关于JQuery选择器:如何选择具有特定类*和* id的项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:29