0

プロジェクトにすべての要素があり、データ属性で転送されているため、javascript は何をすべきかを知ることができます。

しかし、私には1つの問題があります。アプリケーションの一部は、作成されたすべての変数を取得して時間を比較する必要があり、それらを次のように保存しました( data-product-createddata-category-createddata-link-createdなど... )。それらを手動でjQueryセレクターに配置する必要があるとしたら、それは大きな頭痛の種になるでしょう...

jQueryにはカスタムデータ属性の存在を検索する方法がありますか?

お気に入り:element[data-(.*)-created]

4

2 に答える 2

2

低レベルの Javascript メソッドを作成して、要素をループし、作成された値を取得できます。

var getCreateds = function($els) {
    var returnSet = [];
    $els.each(function(i, el){
        var elDict = {'el': el};
        $.each(el.attributes, function(i, attr){
            var m = attr.name.match(/data\-(.*?)\-created/);
            if (m) elDict[m[1]] = attr.value;
        });
        returnSet.push(elDict);
    });
    return returnSet;
};

デモを見る

于 2013-03-06T18:39:50.307 に答える
0

mVChr回答 (ありがとう) に基づいて、コードをシンプルでより優れたクリーンなコードに書き直しました。

jQuery('div').filter(function(){
  for( var i in this.attributes ){
    if(typeof this.attributes[i] !== 'object') continue;
    if(this.attributes[i].name.match(/data\-(.*?)\-created/)) return true;
  }
  return false;
})

変更点:

+ Verifying attribute type... some attributes are browser internal functions or callbacks.
~ Using filter handler from jQuery, its better and cleaner way.
~ Using for instead of $.each, faster indeed.
于 2013-03-06T19:39:25.533 に答える