查找 id 开头的 html 元素

我的问题是:

我在多个页面中使用了 HTML 代码,在每个页面上都使用了 JQgrid (jquery grid)来显示一些数据。我知道在每个页面上,包含 JQgrid 的元素名为“ LIST _ xxx”。现在我需要创建一个 javascript,它在每个页面上接受元素“ LIST _ XXXX”并执行一些操作。如何通过 ID 搜索元素,但只知道它的初始部分(ID 的初始部分,如前所述) :

$('#list_[XXXX]')... --> The part surrounded by [] is variable on each page, i want to discriminate that.

我希望我说得够清楚了,谢谢。

110329 次浏览

Give that element a common class name or some other attribute you can query for.

You need to use the attribute starts with selector, like this:

$('[id^=list_]').whatever()

Try

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

It should work

Use the "attribute starts with" selector http://api.jquery.com/attribute-starts-with-selector/

$("[id^=list_]")

Be aware that this is inefficient. Prefix with the tag name and descend from the nearest parent if possible.

To get an element ending with a certain id/character

find('[id$=someid]')

To get an element starting with a certain id/character

find('[id*=anotherid]')

To get an element matching with a certain id/character

find('[id^=id]')