社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

jquery-选择ID包含特定字符串且属于特定类的div

Jonathan • 5 年前 • 1881 次点击  

JS和JQuery的新功能,将游戏设计为一个练习。

我正在尝试选择ID包含特定字符串并具有特定类的div。

我知道如何选择课程:

$(".myClass")

我知道如何选择包含特定字符串的ID:

$("[id*=" + certainString + "]")

但我不知道如何将两者结合起来得到:

$( elements of class ".myClass" that also have id that contains certainString)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38985
 
1881 次点击  
文章 [ 1 ]  |  最新文章 5 年前
CertainPerformance
Reply   •   1 楼
CertainPerformance    6 年前

只需将选择器放在彼此旁边(中间没有空格),即可选择具有特定类和 有一个 id 匹配约束:

const certainString = 'foo';
const fooMyClasses = $(".myClass[id*=" + certainString + "]");
console.log(fooMyClasses.length);
console.log(fooMyClasses[0].outerHTML);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="myClass">
</div>
<div class="myClass" id="foo123">
</div>