我有这个jQuery代码:

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

但我有几个divs,id是这样的:editDialog-0editDialog-1....,editDialog-n

我如何为所有这divs个,比如上面的一个,生成一个jQuery代码?

推荐答案

Use jquery starts with attribute selector

$('[id^=editDialog]')

Alternative solution - 1 (highly recommended)

一个更干净的解决方案是 for each div&;添加一个公共类;使用

$('.commonClass').

但是,如果html标记不在您手中,您可以使用第一个&;出于某种原因无法更改它.

Alternative solution - 2(如果为n is a large number,则不推荐使用) (根据@Mihai Stancu的建议)

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

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.

Jquery相关问答推荐

jQuery .first() 方法返回第一个元素的集合

在 jQuery 中,我想删除 div 中的所有 HTML

在 Javascript 中启用和禁用 Div 及其元素

如何在 jQuery 中获取浏览器滚动位置?

在单击事件上判断 Ctrl / Shift / Alt 键

$.each(selector) 和 $(selector).each() 有什么区别

获取跨域 iframe 的 DOM 内容

jQuery:如果页面底部有外部 JS,为什么要使用 document.ready?

$(document).on("click"... 不工作?

Bootstrap 4 文件输入

如何使用 javascript 擦除所有内联样式并仅保留 css 样式表中指定的样式?

如何使用 JQuery 获取 GET 和 POST 变量?

如何使用 jQuery 删除 cookie?

使用 JQuery 的黄色淡入淡出效果

表单 propType 失败:您在没有 `onChange` 处理程序的情况下向表单字段提供了 `value` props

在jQuery中,当它们都具有相同的名称时,如何获取单选按钮的值?

fancybox2 / fancybox 导致页面跳转到顶部

如何在 DOM 中移动 iFrame 而不会丢失其状态?

jQuery append() 与 appendChild()

jquery在cookie中保存json数据对象