在 jquery 1.9(及更高版本)中,如何从每个内部获取与元素匹配的选择器的名称?
$("[data='load-something'],.do-something,aside,#something-else").each(function(index,el){
// want to know if this element matched because of the id, class, or data element, etc
});
在 jquery 1.9(及更高版本)中,如何从每个内部获取与元素匹配的选择器的名称?
$("[data='load-something'],.do-something,aside,#something-else").each(function(index,el){
// want to know if this element matched because of the id, class, or data element, etc
});
你可以这样做...
var selectors = ["[data='load-something']",
".do-something",
"aside",
"#something-else"];
$(selectors.join(",")).each(function() {
var element = $(this);
if (element.is(selectors[0]) {
// ...and so forth.
}
});
有点乱,所以如果你能做得更优雅,请做。
查看
$(el).attr("data") //="load-something"
$(el).attr("class") //="do-something"
$(el).prop("tagName") //="aside"
$(el).attr("id"); //="something-else"
尝试这个:
$("[data='load-something'],.do-something,aside,#something-else").each(function(index,el){
if($(this).attr('data')=='load-something')
//called via data
else if($(this).hasClass()=='do-something')
//called via class
else if($(this).prop('tagName')=='aside' || $(this).prop('tagName')=='ASIDE')
//called via aside
else
//called via id
});