使用通配符 ID 选择 div [英] Select div using wildcard ID

查看:28
本文介绍了使用通配符 ID 选择 div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用它的 ID 但使用通配符选择一个 div?

How to select a div using it's ID but with a widcard?

如果 DIV 的 ID 是 statusMessage_1098,我想以某种方式选择它,例如 document.getElementById('statusMessage_*').

If the DIV's ID is statusMessage_1098, I would like to select it in some way like document.getElementById('statusMessage_*').

这是因为在页面生成之前,我不知道ID的后缀,并且一个页面中只会出现一个这样的ID.这可能吗?

This is because until the page is generated, I don't know the suffix of the ID and only one such ID will be present in a page. Is this possible?

感谢您的帮助.

推荐答案

使用 jQuery 你可以做到这一点

Using jQuery you can do this

$("div[id^='statusMessage_']")

参见attributeStartsWith

See attributeStartsWith

编辑 - 使用类名选择器

如果你可以使用类名,那么你可以使用这样的东西

If you can use a class name then you can use something like this

$$('div.myDivClass');

获取所有类 'myDivClass' 的 div 元素

gets all div elements with class 'myDivClass'

这篇关于使用通配符 ID 选择 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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