【发布时间】:2019-01-07 18:05:38
【问题描述】:
如何根据点击的 div 加载新 URL。每个 div 都有自己的加载 URL(在我的实现中,div 是博客页面)。
我的 JQuery: & 我的 html:
$('.postWraper').click(() => {
window.location = $(this).find('h3').find('a').attr('href');
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="postWraper">
<h3 class="postTitleH3">
<a href="blog/first-page" class="postTitle">
<%= post.postTitle %>
</a>
</h3>
</div>
<div class="postWraper">
<h3 class="postTitleH3">
<a href="blog/second-page" class="postTitle">
<%= post.postTitle %>
</a>
</h3>
</div>
当我点击第一个或第二个 div 时,它总是将我重定向到第一个 URL。我不明白为什么。请帮忙!
【问题讨论】:
-
也许你的 html 是坏的或错误的?
-
这不是浏览器的默认行为吗?你为什么要为它写一个脚本?
-
运行代码时,
$(this).find('h3').find('a').attr('href')正在返回undefined
标签: javascript jquery html