2
任何想法,为什么出现这种情况?自定义数据属性选择器不工作
var attr = $(this).data('link');
console.log(attr); // profile_following
console.log($("a[data-target='profile_following']")); // found the object
console.log($("a[data-target='+attr+']")); // [] empty
里面点击处理程序我有上面的线! console.log(attr);
成功打印profile_following
但是,如果我尝试选择一个带有属性选择器的链接,并且这个变量像这样console.log($("a[data-target='+attr+']"));
它找不到该元素!
而最奇怪的是,如果我硬编码线那样console.log($("a[data-target='profile_following']"));
它找到对象成功。
任何想法为什么同一行不会在属性选择器中使用+attr+
?
你必须在选择器中注入'attr',所以你必须用双引号(''a [data-target ='“+ attr +''”“')来分隔前缀和后缀字符串。此外,这是[jQuery如何找到基于数据属性值的元素的副本?](http://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-上一个数据属性值) – 2012-02-14 18:26:18