jquery for each all elements having similar id

雨燕双飞 提交于 2019-12-21 07:34:24

问题


I have many elements accross page - ID1, ID2 ID3 ...

I want to manipulate all the elements. Is there any simpler way to do this.

$("#ID").each(function(){ ... });

回答1:


You can use ^ selector.

Example

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

^= select DOM whose ID attribute starts with ID (i.e ID1, IDID,IDS,ID2 etc)




回答2:


Give them a class, so you can select them by the class?

$('.class').each(function(i,e) { // });



回答3:


If the ID portion isn't necessarily at the beginning you could do:

$( "[tagName][id*='ID']" )

Here's a full list of selectors: https://api.jquery.com/category/selectors/




回答4:


$('element[id^="ID"]').each(function () {
console.log(this.value);
});

Where element is the name of your targeted html element.




回答5:


  function(ID)
  {
        ... $("#ID"+ID) ...
  }

  for (i=1;i<3;i++)
  {
       function(i);
  }


来源:https://stackoverflow.com/questions/16191727/jquery-for-each-all-elements-having-similar-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!