使用window.location-string的Javascript切换问题
作者:互联网
我目前正在研究一个简单的ipad webapp测试版,并且卡在一个简单的开关语句中,不能以某种方式工作;
我想根据javascripts window.location字符串格式化元素.这可能很容易解决;但我不明白得到它:
或者window.location有什么特别之处吗?
$(document).ready(function() {
path_c = window.location;
switch (path_c){
case "http://192.168.1.37/ryba_testground/":
$('#menu-item-22').addClass('current_page_item');
alert(path_c);
break;
case "http://192.168.1.37/ryba_testground/?page_id=7":
$('#menu-item-21').addClass('current_page_item');
break;
case "http://192.168.1.37/ryba_testground/?page_id=9":
$('#menu-item-20').addClass('current_page_item');
break;
case "http://192.168.1.37/ryba_testground/?page_id=11":
$('#menu-item-19').addClass('current_page_item');
break;
}
});
谢谢!
解决方法:
Or is there something special about the window.location?
它是一个宿主对象,有时候行为不可预测.至少,它是一个在转换为字符串时计算href的对象 – 但是在与switch-cases进行比较时不会发生这种情况.使用
var path_c = window.location.href;
标签:window-location,javascript,switch-statement 来源: https://codeday.me/bug/20190901/1782525.html