简体   繁体   English

通过jquery选择具有类名称的特定字符串的所有元素

[英]Select all elements with class name start by specific string by jquery

This is my HTML DOM: 这是我的HTML DOM:

<div id="d1" class="Addable"><span class="yak-bak-nak vok-do-maria"> <p>Goola1</p>
<span>volaa</span></span></div>

<div id="d2" class="Addable"><span class="yak-bak-val vok-do-maria"> <p>Goola2</p></span></div>

<div id="d3" class="Addable"><span class="vok-do-maria yak-bak-nak"> <p>Goola3</p></div>

<div id="d4" class="Addable"><span class="vok-do-maria yak-bak-nak"> <p>Goola4</p></div>

<input type="button" class="Doop" value="Do" />

So I want to remove all divs that class names start with "yak-bak" that means all of the above. 所以我想删除所有类名以“yak-bak”开头的divs ,这意味着以上所有。

so I try this script: 所以我试试这个脚本:

$('.Doop').click(function () {

    $('span[class|="yak-bak"]').parent('div').remove();
});

but just d1 and d2 remove and d3, d4 remain because the yak-bak class come as second also I try this: 但只是d1和d2删除而d3,d4仍然存在,因为yak-bak类也是第二个我也尝试这个:

$('.Doop').click(function () {

    $('span[class^="yak-bak"]').parent('div').remove();
});

So what is your suggestion? 那你的建议是什么? where is the problem? 问题出在哪儿?

To target elements with multiple classes, where one of then starts by "yak-bak" at the beginning of the attribute class, or at the middle of it, you can use: 要定位具有多个类的元素,其中一个类在属性类的开头是“yak-bak”,或者在其中间,可以使用:

JQUERY JQUERY

$("div[class^='yak-bak'],div[class*=' yak-bak']").parent('div').remove();

This will select elements where the class starts by "yak-bak" and elements that contain a class stated by "yak-bak". 这将选择类以“yak-bak”开头的元素和包含“yak-bak”所述类的元素。

$('div').filter(function() {
  return $(this).has('span[class^="yak-bak"]');
}).remove();

DEMO DEMO

Or 要么

$('div').filter(function() {
  return $(this).has('span[class^="yak-bak"], span[class*=" yak-bak"]');
}).remove();

But First one will enough. 但第一个就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM