在 onclick 中更改 location.hash 时,它会更改为 undefined [英] When changing location.hash in onclick it changes to undefined
本文介绍了在 onclick 中更改 location.hash 时,它会更改为 undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:
$(e).click(function() {
console.log(this.href);
location.hash = this.href;
});
这里的 e
是一个 元素,如下所示:
<li href="#about">About</li>代码>
location.hash
有 onchange 监听器:
Here e
is a <li>
element like this: <li href="#about">About</li>
location.hash
has onchange listener:
$(window).hashchange(function() {
if (location.hash=="") location.hash="me";
$(".content").spin();
$(".content").load("http://example.com/inc/"+location.hash.substr(1), function() {
$(this).spin(false);
});
});
UPD:问题不在于变量访问,因为 console.log 打印变量的正确值.
推荐答案
href is not a valid attribute of LI
href is not a valid attribute of LI
将 data-href(或者可能更好:data-hash)添加到您的 LI
Add data-href (or perhaps better:data-hash) to your LI
<li data-href="somevalue">...
并使用
$(e).click(function() {
location.hash = $(this).data("href");
});
这篇关于在 onclick 中更改 location.hash 时,它会更改为 undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文