id的jquery选择器以特定文本开头 [英] jquery selector for id starts with specific text

查看:135
本文介绍了id的jquery选择器以特定文本开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个jQuery代码:

I have this jQuery code:

$( "#editDialog" ).dialog({
  autoOpen: false,
  show: {
    effect: "blind",
    duration: 1000
  },
  hide: {
    effect: "explode",
    duration: 1000
  }
});

但我有几个id为id的div:editDialog-0,editDialog-1,... 。,editDialog-n。

But I have several divs with id's like this: editDialog-0, editDialog-1, ...., editDialog-n.

如何为所有这些div创建一个jQuery代码,如上面那个?

How can I make a jQuery code for all of these divs, like the one above?

推荐答案

使用jquery 属性选择器

Use jquery starts with attribute selector

$('[id^=editDialog]')

替代解决方案 - 1(强烈推荐)

更清洁的解决方案是为每个div添加一个公共类&使用

A cleaner solution is to add a common class to each of the divs & use

$('。commonClass')

但如果html标记不在您的手中,您可以使用第一个由于某些原因无法改变它。

But you can use the first one if html markup is not in your hands & cannot change it for some reason.

替代解决方案 - 2 (如果 n是一个大数字,则不推荐

Alternative solution - 2 (not recommended if n is a large number) (as per @Mihai Stancu's suggestion)

$('#editDialog-0,#editDialog- 1,#editDialog-2,...,#editDialog-n')

注意:如果有2个或3个选择器,如果列表不会改变,这可能是一个可行的解决方案,但它不可扩展,因为我们必须在城镇中有新的ID时更新选择器。

Note: If there are 2 or 3 selectors and if the list doesn't change, this is probably a viable solution but it is not extensible because we have to update the selectors when there is a new ID in town.

这篇关于id的jquery选择器以特定文本开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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