CSSプロパティでDOMエレメントを選択できますか?例えばJavaScriptのCSSプロパティでエレメントを選択
:
awsome_function ({'background-color' : '#0cf'});
// return all object, which's background-color css attribute value is '#0cf'
awsome_function (['background-color']);
// return all object, which has background-color css attribute
awsome_function (['-my-special-cssattribute'])
// return all object, which has '-my-special-cssattribute' css attribute
私はこのように、jQueryのeach
方法を使用して要素を選択することが可能であることを知っている:それは多分遅いとunelegantだが
$('*').each(function(){
if($(this).css('-my-special-cssattribute')) {
/* do something */
}
})
。それを行うためのよりクールな方法はありますか?
私はそれが大好きです。その 'foo'をパラメータ化する方法はありますか?たとえば、$( 'div:foo( "background-color"、 "#0cf")') –
@NagyMárton必要な要素を返す関数の内部に、この種のロジックを保持することをお勧めします。条件を追加したい場合は、 '&&'を使用してください。 – Johan
ありがとうございます! –