【发布时间】:2023-03-13 19:57:01
【问题描述】:
我有一个 div,里面有一个 a。我想单击 div 并遵循与 a.相同的 url。这是 HTML:
<div class="overview white getchildurl">
<p class="w70">
05-02-2011
</p>
<a class="button" href="details/">Details</a>
<span class="clear"></span>
</div>
我有这个脚本:
$('.getchildurl').each(function() {
$(this).click(function() {
var href = $(this).children('a').first().attr('href');
window.location = href;
});
});
这适用于 Firefox 3.6、Chrome、Safari,但不适用于 IE7 和 IE8。它重新加载页面,但保持在同一页面上。我检查了 var href,它有正确的 url,但没有去那里。我做错了什么?
谢谢。
编辑:使其成为绝对 URL 使其工作。这是新脚本:
$('.getchildurl').each(function() {
$(this).click(function() {
var href = $(this).children('a').first().attr('href');
var host = window.location.hostname;
window.location = 'http://' + host + '/' + href;
});
});
【问题讨论】:
-
尝试显式设置
href属性。window.location.href = href; -
window.location.href 可能有效!
-
为我工作:jsfiddle.net/ACaKu 你能去那里检查一下吗?一些事情要尝试:从“details/”中删除尾部斜杠,
location.href = href,使用绝对路径。尝试从 url 栏中输入javascript: window.location='details/'; void(0); -
window.location.href 也不能正常工作。它适用于除 IE7/8 以外的所有浏览器。删除尾部斜杠不是一个选项,网址是动态添加的,我不控制它的语法。
标签: javascript jquery xhtml