admin管理员组文章数量:1317564
I want to use inline loading (via AJAX) on my website, while at the same time preserving (some) functionality for people without access to Javascript (if they still exist).
My menu's:
<?php
echo "<h3 id='title'></h3>";
if (empty($_SESSION['uid'])) {
echo "<a href='index'>Home</a> <br> ";
echo "<a href='register'>Register</a> <br> ";
echo "<a href='login'>Log In</a> <br> ";
echo "<a href='browser'>Browse MarketPlace</a> <br> ";
echo "<a href='support'>Support us!</a>";
}
elseif (!empty($_SESSION['uid'])) {
echo "<a href='index'>Home</a> <br> ";
echo "<a href='overview'>Account Overview</a> <br> ";
echo "<a href='browser'>Browse MarketPlace</a> <br> ";
echo "<a href='sell'>Sell Items</a> <br> ";
echo "<a href='logout'>Logout</a> <br> ";
echo "<a href='support'>Support us!</a>";
}
?>
My Javascript:
$(document).ready(function() {
$(document).on("click", "a", function(event) {
var dataUrl = $(this).attr("href");
if (dataUrl != "") {
loadPage(dataUrl);
}
return false;
});
});
Is this the correct way to do it? I am especially worried about the need to block the original a href
call, the native browser one.
It has worked entirely fine when I used something custom like <a data-url='index'>
. Then I tried the new version and it did not work as expected without return false;
, but now it does, so I'm not too sure about that.
I want to use inline loading (via AJAX) on my website, while at the same time preserving (some) functionality for people without access to Javascript (if they still exist).
My menu's:
<?php
echo "<h3 id='title'></h3>";
if (empty($_SESSION['uid'])) {
echo "<a href='index'>Home</a> <br> ";
echo "<a href='register'>Register</a> <br> ";
echo "<a href='login'>Log In</a> <br> ";
echo "<a href='browser'>Browse MarketPlace</a> <br> ";
echo "<a href='support'>Support us!</a>";
}
elseif (!empty($_SESSION['uid'])) {
echo "<a href='index'>Home</a> <br> ";
echo "<a href='overview'>Account Overview</a> <br> ";
echo "<a href='browser'>Browse MarketPlace</a> <br> ";
echo "<a href='sell'>Sell Items</a> <br> ";
echo "<a href='logout'>Logout</a> <br> ";
echo "<a href='support'>Support us!</a>";
}
?>
My Javascript:
$(document).ready(function() {
$(document).on("click", "a", function(event) {
var dataUrl = $(this).attr("href");
if (dataUrl != "") {
loadPage(dataUrl);
}
return false;
});
});
Is this the correct way to do it? I am especially worried about the need to block the original a href
call, the native browser one.
It has worked entirely fine when I used something custom like <a data-url='index'>
. Then I tried the new version and it did not work as expected without return false;
, but now it does, so I'm not too sure about that.
- 1 api.jquery./event.preventDefault – Blazemonger Commented Dec 5, 2013 at 17:04
-
2
use
event.preventDefault()
– Pranav C Balan Commented Dec 5, 2013 at 17:04
2 Answers
Reset to default 9use event.preventDefault();
If this method is called, the default action of the event will not be triggered.
$(document).ready(function() {
$(document).on("click", "a", function(event) {
event.preventDefault();
var dataUrl = $(this).attr("href");
if (dataUrl != "") {
loadPage(dataUrl);
}
});
});
Try using
event.preventDefault()
This cancels the event if it is cancelable, without stopping further propagation of the event.
Refer event.preventDefault()
本文标签: javascriptjQuery block a hrefStack Overflow
版权声明:本文标题:javascript - jQuery block a href - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742013256a2413288.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论