通过jquery选择具有类名称的特定字符串的所有元素 [英] Select all elements with class name start by specific string by jquery

查看:82
本文介绍了通过jquery选择具有类名称的特定字符串的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML DOM:

This is my 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" />

所以我想删除所有 divs 那个类名以yak-bak开头,表示以上所有。

So I want to remove all divs that class names start with "yak-bak" that means all of the above.

所以我尝试这个脚本:

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

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

但只是d1和d2删除而d3,d4仍然是因为 yak- bak 上课第二次我也试试这个:

but just d1 and d2 remove and d3, d4 remain because the yak-bak class come as second also I try this:

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

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

那你的建议是什么?问题出在哪里?

So what is your suggestion? where is the problem?

推荐答案

要定位具有多个类的元素,其中一个以yak-bak开头属性类的开头,或者在其中间,您可以使用:

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:

JQUERY

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

这将选择以yak-bak开头的元素和包含的元素由yak-bak表示的课程。

这篇关于通过jquery选择具有类名称的特定字符串的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆