【问题标题】:jQuery selector for id starts with specific text [duplicate]id的jQuery选择器以特定文本开头[重复]
【发布时间】:2020-09-28 22:31:53
【问题描述】:

我有这个 jQuery 代码:

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

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

我怎样才能为所有这些 divs 制作一个 jQuery 代码,就像上面的一样?

【问题讨论】:

  • 您可以像使用 CSS 选择器 "#Id1, #id2" 等...

标签: javascript jquery


【解决方案1】:

使用 jquery starts with 属性选择器

$('[id^=editDialog]')

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

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

$('.commonClass')。

但如果 html 标记不在您手中并且由于某种原因无法更改,您可以使用第一个。

替代方案 - 2(不推荐n is a large number) (根据@Mihai Stancu 的建议)

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

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

【讨论】:

  • @Krishna 为了完整起见,如果您想在您的答案中添加我的评论(列出多个 CSS 选择器):jQuery('#anID1, #anID2, #anID2')
  • 有谁知道,如果我用 ajax 加载 div 框,我怎样才能让这段代码工作?
  • 避免仅按类选择:$(".myclass");将在现代浏览器中快速运行。不幸的是,在 IE6/7 和 Firefox 2 等旧版浏览器中,jQuery 必须检查页面上的每个元素以确定是否应用了“myclass”。如果我们使用标签名称来限定选择器,则选择器将更有效,例如: $("div.myclass");
  • $('#editDialog-0, #editDialog-1, #editDialog-2,...,#editDialog-n') 也可以写成$('#editDialog-0').add('#editDialog-1').add('#editDialog-2') 等('#editDialog-n') 注意我可能会为任何超出琐碎长度的集合寻求另一种解决方案。
【解决方案2】:

考虑到你还没有提到但会发现有用的东西,让我提供一个更广泛的答案。

对于您当前的问题,答案是

$("div[id^='editDialog']");

插入符号 (^) 取自正则表达式,表示 starts with。

解决方案 1

// Select elems where 'attribute' ends with 'Dialog'
$("[attribute$='Dialog']"); 

// Selects all divs where attribute is NOT equal to value    
$("div[attribute!='value']"); 

// Select all elements that have an attribute whose value is like
$("[attribute*='value']"); 

// Select all elements that have an attribute whose value has the word foobar
$("[attribute~='foobar']"); 

// Select all elements that have an attribute whose value starts with 'foo' and ends
//  with 'bar'
$("[attribute^='foo'][attribute$='bar']");

上面代码中的attribute可以更改为元素可能具有的任何属性,例如href、name、id或src。

解决方案 2

使用类

// Matches all items that have the class 'classname'
$(".className");

// Matches all divs that have the class 'classname'
$("div.className");

解决方案 3

列出它们(在之前的答案中也提到过)

$("#id1,#id2,#id3");

解决方案 4

当你改进的时候,正则表达式(从来没有实际使用过这些,解决方案一一直都足够了,但你永远不知道!

// Matches all elements whose id takes the form editDialog-{one_or_more_integers}
$('div').filter(function () {this.id.match(/editDialog\-\d+/)});

【讨论】:

  • 作为$("[attribute^='foo'][attribute$='bar']"); 的替代方案 - 这就是说找到所有以“bar”结尾的属性,然后在该集合中找到那些以“foo”开头的属性,因为从右到左处理嘶嘶作响。也许一个改进可能是$("[attribute^='foo']").filter("[attribute$='bar']");,它允许您以“foo”隔离开头,然后在带有后缀“bar”的集合中进行选择——这可能会更快。结果取决于您的 DOM 和每个的频率。
【解决方案3】:

如果您的所有 div 都以您所说的 editDialog 开头,那么您可以使用以下选择器:

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

或者你可以使用类选择器来代替,如果它更容易的话

<div id="editDialog-0" class="editDialog">...</div>

$(".editDialog")

【讨论】:

    【解决方案4】:

    为所有的 div 添加一个公共类。例如将 foo 添加到所有 div。

    $('.foo').each(function () {
       $(this).dialog({
        autoOpen: false,
        show: {
          effect: "blind",
          duration: 1000
        },
        hide: {
          effect: "explode",
          duration: 1000
        }
      });
    });
    

    【讨论】:

    • 要么在 jQuery 选择器中列出 id,要么在 jQuery 选择器中列出:jQuery('#myid1, #myid2, #myid3')。
    • 这需要更改 HTML,但并非绝对必要。
    • @Mihai Stancu,如果有368个ID怎么办?
    • 也许是这样,但如果你必须枚举 id,你可能一开始就不需要 id。
    • @isherwood 你说的是真的,但这是一种推荐的做事方式——如果它们有共同的功能,它们也应该有共同的类。
    猜你喜欢
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    相关资源
    最近更新 更多