Javascript变量。。。can';t参考
有人能告诉我为什么这个参考不起作用吗Javascript变量。。。can';t参考,javascript,jquery,Javascript,Jquery,有人能告诉我为什么这个参考不起作用吗 var a_nav_href = $(this).attr("data-id"); //get the id data when click in a tag**strong text** $(".wrapper").find("section[id=a_nav_href]").css({"background-color": "red"}); 您的代码查找id=a\u nav\u href 你想要: $(".wrapper").find("section
var a_nav_href = $(this).attr("data-id"); //get the id data when click in a tag**strong text**
$(".wrapper").find("section[id=a_nav_href]").css({"background-color": "red"});
您的代码查找
id=a\u nav\u href
你想要:
$(".wrapper").find("section[id=" + a_nav_href + "]").css({"background-color": "red"});
你写了:
var a_nav_href = $(this).attr("data-id");
$(".wrapper").find("section[id=a_nav_href]").css({"background-color": "red"});
在此代码段中,a_nav_href“是选择器的一部分,但作为字符串。您需要将_nav_href的值附加到选择器
换成
$(".wrapper").find("section[id=" + a_nav_href + "]").css({"background-color": "red"});
这将返回如下所示的选择器查询
$(".wrapper").find("section[id=someIDWhichIWantToSearch]").css({"background-color": "red"});
非常感谢你,你让我开心@Tomasz Jakub非常感谢你…我做到了!欢迎来到堆栈溢出!请拿着这本书,四处看看,特别是通读一下