通过id属性的一部分查找所有元素 [英] Find all elements by part of the id attribute

查看:94
本文介绍了通过id属性的一部分查找所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否可行:

我有一组div,每个都有一个以'_font'结尾的ID,例如'body_font','heading_font ','tagline_font'等等。

I have a set of divs, each with an ID ending in '_font', e.g 'body_font', 'heading_font', 'tagline_font', etc.

有没有办法通过搜索他们名字的公共部分来获取这些元素,在这种情况下'_font'所以我可以后来用jQuery操作它们?

Is there a way to grab those elements by searching for a common portion of their names, in this case '_font' so I can later manipulate them using jQuery?

推荐答案

你可以使用属性结束 - 使用选择器

var elems = $("div[id$='_font']");

如果你花一些时间浏览jQuery API,你应该能够自己回答这样的问题而不必在Stack Overflow上发布。

If you spend some time browsing the jQuery API you should be able to answer questions like this yourself without having to post on Stack Overflow.

其他可能有用的选择器:

Other selectors that might come in useful:

  • Attribute starts-with selector
  • Attribute contains selector
  • Attribute equals selector

这篇关于通过id属性的一部分查找所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆