jQuery/html 中的嵌套链接
Nested links in jQuery/html
这是我的代码:
<li>
<a href="{{ path('homepage') }}" class="active">
<i class="fa fa-tasks fa-fw"></i>
{% trans %} global.item {% endtrans %} <span id="pending-badge" class="badge pull-right">{{ pendingItems() }}</span>
</a>
</li>
我也有这个 jQuery:
$('#pending-badge').click(function () {
window.location.href = '{{ path('pending') }}';
});
但是我注意到点击徽章实际上并没有将我重定向到那个页面。
如果我将 console.log('test');
放在 jQuery 函数中,我会在控制台中看到它,所以我知道事件正在注册。但我认为因为它发生在更广泛的 <a>
link 中,所以它只是默认为 (homepage
) 而不是我提供的 link (pending
) jQuery.
我该如何解决这个问题?
您可以通过 return false
或调用 event.preventDefault()
来阻止默认操作
$('#pending-badge').click(function (e) {
window.location = 'http://google.com';
return false;
});
这是我的代码:
<li>
<a href="{{ path('homepage') }}" class="active">
<i class="fa fa-tasks fa-fw"></i>
{% trans %} global.item {% endtrans %} <span id="pending-badge" class="badge pull-right">{{ pendingItems() }}</span>
</a>
</li>
我也有这个 jQuery:
$('#pending-badge').click(function () {
window.location.href = '{{ path('pending') }}';
});
但是我注意到点击徽章实际上并没有将我重定向到那个页面。
如果我将 console.log('test');
放在 jQuery 函数中,我会在控制台中看到它,所以我知道事件正在注册。但我认为因为它发生在更广泛的 <a>
link 中,所以它只是默认为 (homepage
) 而不是我提供的 link (pending
) jQuery.
我该如何解决这个问题?
您可以通过 return false
或调用 event.preventDefault()
$('#pending-badge').click(function (e) {
window.location = 'http://google.com';
return false;
});