是否可以通過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
方法選擇元素,像這樣:
$('*').each(function(){
if($(this).css('-my-special-cssattribute')) {
/* do something */
}
})
但是它也許慢,unelegant。有沒有更酷的方法來做到這一點?
我喜歡它。有什麼辦法可以參數化'foo'嗎? forexample $('div:foo(「background-color」,「#0cf」)') – 2012-02-27 12:07:33
@NagyMárton我建議在函數內部保留這種邏輯,以返回你需要的元素。如果你想添加更多條件,請使用'&&' – Johan 2012-02-27 12:22:55
非常感謝! – 2012-02-27 12:26:12