jQuery哈希检测Internet Explorer
jQuery Hash Detection Internet Explorer
我正在使用jQuery构建导航选项卡,并且正在检测URL散列值,以用作页面加载时导航选项卡的参考。例如,当有人访问:example.com/profile.php#media时,"媒体"选项卡会自动切换到。
我的jQuery代码可以在Safari, Firefox, Chrome和Opera中工作,但不能在任何版本的Internet Explorer(测试IE 6 - 10)中工作。我的代码中是否有任何东西使其与IE不兼容?
JavaScript:$(document).ready(function() {
tab = $('.tab');
function switch_active_tab() {
tab.removeClass('active_tab');
$(this).addClass('active_tab');
}
function hash_detect() {
hash = document.location.hash.replace('#','');
active_tab_id = $('.active_tab').attr('id').replace('-manager', '');
// check if hash value is valid
if( hash == 'pages' || hash == 'media' || hash == 'templates' || hash == 'scripts' ) {
// if hash is not the same as the active tab
if( hash !== active_tab_id ) {
tab.removeClass('active_tab');
$(document.location.hash+'-manager').addClass('active_tab');
}
}
else {
document.location = '#pages';
}
}
function hash_respond() {
hash = document.location.hash.replace('#','');
active_tab_id = $('.active_tab').attr('id').replace('-manager', '');
if( hash !== active_tab_id ) {
document.location = '#' + active_tab_id.replace('-manager', '');
}
}
$(document).ready(hash_detect);
$(window).bind('hashchange', hash_detect);
tab.click(switch_active_tab);
tab.click(hash_respond);
});
相应的HTML :
<div id="tab_wrapper">
<div class="tab active_tab" id="pages-manager">Pages</div>
<div class="tab" id="media-manager">Media</div>
<div class="tab" id="templates-manager">Templates</div>
<div class="tab" id="scripts-manager">Scripts</div>
</div>
根据MSDN, location
对象属于window
。
我看了一下我的一些使用hash
的代码,我只参考location.hash
,从来没有遇到过IE的问题。我怀疑你可以尝试使用window.location.hash
或location.hash
。
我看到的另一个问题是您没有使用var
关键字声明任何变量。IE积极致力于此。所有的变量都应该这样声明:
var hash; // simple declaration
还是……
var hash = window.location.hash.replace('#', ''); // declaration with assignment
也有可能IE在完全不相关的事情上出现问题。我会检查确保active_tab_id
正在获得您期望的值。
作为最后的手段,你应该使用IE开发人员工具。调试器并不是所有这些,但它有时是有用的。
相关文章:
- 视频HTML没有'无法在Internet Explorer 11上工作
- Internet Explorer缺少占位符支持,特别是密码字段
- Internet Explorer中的文本换行错误
- 为什么我在Internet Explorer上看不到html元素
- internet explorer 8兼容模式下的Youtube api错误
- 当浏览器控制台未打开时,为什么要求会导致Internet Explorer 9超时
- Internet Explorer 8. Events
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- element.dataset in Internet Explorer
- Facebook FB.init”;访问被拒绝”;在Internet Explorer 11中
- JavaScript不适用于internet explorer和Firefox,但适用于谷歌
- 在Internet Explorer中,向所选内容添加选项不起作用
- 在Internet Explorer中从二进制文件打开PDF
- elementproblem internet explorer
- 我可以使用哪些方法在Internet Explorer中禁用Alt+*X*
- 如何在Internet Explorer中使用javascript设置cookie
- 只有当alert()在带有AJAX的internet explorer中使用时,Javascript才会更新UI
- sharepoint站点的internet explorer中出现Javascript错误
- Internet Explorer重新加载IFrame
- Internet Explorer Internet Failure HTTP Status Codes