Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript变量。。。can';t参考_Javascript_Jquery - Fatal编程技术网

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非常感谢你…我做到了!欢迎来到堆栈溢出!请拿着这本书,四处看看,特别是通读一下