php*_*00b 1 javascript jquery if-statement class set
我正在尝试根据网址将类设置为活动状态.我正在尝试使用下面的代码,但在每种情况下,它都会激活第二个选项卡的活动类.
var pathname = window.location.pathname;
if(pathname = '/learn/subsection2') {
$("ul.tabs li:eq(1)").addClass("active").show(); //Activate second tab
$(".tab_content:eq(1)").show(); //Show second tab content
} else {
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
}
Run Code Online (Sandbox Code Playgroud)
您在if语句中分配而不是检查是否相等.
if(pathname == '/learn/subsection2') {
...
Run Code Online (Sandbox Code Playgroud)
if(pathname = '/learn/subsection2') { // assignment
if(pathname == '/learn/subsection2') { // test for equality
if(pathname === '/learn/subsection2') { // test for equality without type coercion
Run Code Online (Sandbox Code Playgroud)