在JQuery中,如何选择id以“aaa”开头、类“class_name”的所有按钮?

时间:2022-11-28 09:31:29

How can I select all buttons which id starts with "aaa" that have a class which name is class_name1?

我如何选择所有以“aaa”开头的按钮,其中有一个名为class_name1的类?

Summary:

简介:

  • select all element which id start with "aaa"
  • 选择以“aaa”开头的所有元素
  • inside this group select all button which have a class called class_name1
  • 在这个组中,选择具有类class_name1的all按钮
  • remove this class and add a new class class_name2.
  • 删除这个类并添加一个新的类class_name2。

The last step should be:

最后一步应该是:

$('#id').removeClass("class_name1").addClass("class_name2");

2 个解决方案

#1


2  

See jQuery ID starts with

参见jQuery ID

$('button[id^="aaa"].class_name1').removeClass("class_name1").addClass("class_name2");

button[id^="aaa"] looks for buttons with ids starting with 'aaa'. .class_name1 limits those to elements with the class class_name1.

按钮[id ^ = " aaa ")和id查找按钮开始“aaa”。.class_name1 class_name1限制这些元素类。

#2


1  

In JQuery, there is a class selector. For example, if you wanted all elements with class = class1, you would use $('.class1').each(function(){...});

在JQuery中,有一个类选择器。例如,如果想要class = class1中的所有元素,可以使用$('.class1').each(function(){…});

Within this each() function, you could use the JQuery wildcard selector to get all elements with an id starting with 'aaa':

在这个each()函数中,您可以使用JQuery通配符选择器获取所有id以“aaa”开头的元素:

if($('[id^=aaa]')){ $('.class1').addClass('class2'); $('.class1').removeClass('class1'); } });

如果($('[id ^ = aaa]')){ $(' .class1 ').addClass(类别2);$(' .class1 ').removeClass(class1的);} });

I believe you will need to take care of those add and remove methods on separate lines in order for this to work.

我相信您将需要在单独的行上处理添加和删除方法,以使其工作。

Hope this helps.

希望这个有帮助。

#1


2  

See jQuery ID starts with

参见jQuery ID

$('button[id^="aaa"].class_name1').removeClass("class_name1").addClass("class_name2");

button[id^="aaa"] looks for buttons with ids starting with 'aaa'. .class_name1 limits those to elements with the class class_name1.

按钮[id ^ = " aaa ")和id查找按钮开始“aaa”。.class_name1 class_name1限制这些元素类。

#2


1  

In JQuery, there is a class selector. For example, if you wanted all elements with class = class1, you would use $('.class1').each(function(){...});

在JQuery中,有一个类选择器。例如,如果想要class = class1中的所有元素,可以使用$('.class1').each(function(){…});

Within this each() function, you could use the JQuery wildcard selector to get all elements with an id starting with 'aaa':

在这个each()函数中,您可以使用JQuery通配符选择器获取所有id以“aaa”开头的元素:

if($('[id^=aaa]')){ $('.class1').addClass('class2'); $('.class1').removeClass('class1'); } });

如果($('[id ^ = aaa]')){ $(' .class1 ').addClass(类别2);$(' .class1 ').removeClass(class1的);} });

I believe you will need to take care of those add and remove methods on separate lines in order for this to work.

我相信您将需要在单独的行上处理添加和删除方法,以使其工作。

Hope this helps.

希望这个有帮助。