使用通配符号来选择div [英] Select div using wildcard ID
问题描述
如何使用ID进行选择,但使用宽卡?
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_']")
See attributeStartsWith
编辑 - 使用类名选择器
Edit - with class name selector
如果你可以使用类名,那么你可以使用这样的东西
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'
这篇关于使用通配符号来选择div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!