如何使用属性名称的一部分查找html属性

时间:2014-02-20 12:18:43

标签: javascript jquery html

我需要找到html页面中包含“data”的所有属性。当我有这些时,我需要删除它们(或者只删除它中的所有值)。有人有建议吗?

示例:我的html如下:

<section id="slide-2">
                <div class="bcg" data-center="background-color: #f0f0f0">
                    <div class="hsContainer">
                        <div class="hsContent">

                            <div class="container">
                                <div class="row">
                                    <div class="span12">
                                        <header data-center="opacity: 1" data-100-top="opacity: 1" data-1-top="opacity: 0">
                                            <img src="img/headers/pencil.png" alt="pencil">
                                            <h1>Het concept</h1>
                                        </header>

                                        <p data-center="opacity: 1" data-100-top="opacity: 1" data-1-top="opacity: 0">“Het creëren van een omgeving die de mens afsluit van alle schadelijke straling en radiatie op een unieke stralingsvrije locatie in België.“
                                        <br/> Gesteund door Toerisme Vlaanderen.
                                        </p>

                                        <div id="logobox" data-center="opacity: 1" data-100-top="opacity: 1" data-1-top="opacity: 0">
                                            <img src="img/concept/logo.png" alt="logo">
                                            <img src="img/concept/toerismevlaanderen.png" alt="">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

我需要获取名称中包含“data”的所有属性,并清除属性的值。

3 个答案:

答案 0 :(得分:1)

尝试

$('*').each(function(){
    var elem = $(this);
    $.each(this.attributes, function(key){
        if (key.indexOf('data') > -1){
            elem.attr(key, '');
        }
    });
});

答案 1 :(得分:-1)

你可以借助html5的数据集api更喜欢jsfiddle

    var element  = document.getElementsById('myDiv');
    var datasets = element.dataset;

    $.each(datasets,function(key,val){element.dataset.key = '';});

http://jsfiddle.net/adeshpandey/N6W8s/3/

注意:请在使用此代码之前加载jquery。 :P

答案 2 :(得分:-1)

使用$(":contains('data')")

OR

您也可以使用以下链接:https://github.com/mathiasbynens/jquery-custom-data-attributes

希望这会有所帮助。祝一切顺利。! :)