[英]How to remove hash in link inside anchor tag when ajax is calling the page to load in div
Hi every one i calling the page through ajax call to load in a particular div. 大家好,我通过ajax调用来调用页面以加载特定的div。
<ul id="nav" class="nav" style="font-size:12px;">
<li><a href="#" id="m_blink" onclick="fun1()">tab1</a></li>
<li><a href="#" id="d_blink" onclick="fun2()">tab2</a></li>
<li><a href="#" id="k_blink" onclick="fun3()">tab3</a></li>
</ul>
<div id="home"></div>
This is my Javascript script calling the pages in a particular 'home' div 这是我的Javascript脚本,用于调用特定“ home” div中的页面
<script type="text/javascript">
function fun1(){
$("#home").load("page1.php",{},function(){});
}
function fun2(){
$("#home").load("page2.php",{},function(){});
}
function fun3(){
$("#home").load("page3.php",{},function(){});
}
</script>
And when i called the pages in 'home' div : 当我在'home'div中调用页面时:
In the URl : 在URl中:
when i clicked on the tab1 当我单击tab1时
localhost/site/#
Even when i clicked on the tab2 即使我单击了tab2
localhost/site/#
And when i clicked on the tab3 当我点击tab3时
localhost/site/#
I want to remove the # value on the URL ,Any one please suggest me how to remove the hash value from the URL. 我想删除网址上的#值,有人建议我如何从网址中删除哈希值。
Thanks 谢谢
Try this solution ;) 试试这个解决方案;)
<ul id="nav" class="nav" style="font-size:12px;">
<li><a href="#" id="m_blink">tab1</a></li>
<li><a href="#" id="d_blink">tab2</a></li>
<li><a href="#" id="k_blink">tab3</a></li>
</ul>
<div id="home"></div>
<script type="text/javascript">
$("#m_blink").click(function(event) {
event.preventDefault();
$("#home").load("page1.php", {}, function() {});
});
$("#d_blink").click(function(event) {
event.preventDefault();
$("#home").load("page2.php", {}, function() {});
});
$("#k_blink").click(function(event) {
event.preventDefault();
$("#home").load("page3.php", {}, function() {});
});
</script>
The click handler should return false so the link isn't followed if the JavaScript doesn't fail 点击处理程序应返回false,因此如果JavaScript没有失败,则不会遵循链接
<script type="text/javascript">
function fun1(){
$("#home").load("page1.php",{},function(){});
return false;
}
function fun2(){
$("#home").load("page2.php",{},function(){});
return false;
}
function fun3(){
$("#home").load("page3.php",{},function(){});
return false;
}
</script>
You could refactor the whole script into something like: 您可以将整个脚本重构为以下形式:
It's also worth noting that it's a good idea to separate the Javascript from HTML, so you don't have inline click events being called. 还值得注意的是,将Javascript与HTML分开是个好主意,因此不必调用内联单击事件。
$(document).ready(function() {
$("#nav a").on("click", function(e){
e.preventDefault();
var page = $(this).attr("href");
$("#home").load(page);
});
});
And the preventDefault();
和
preventDefault();
function will stop the # being added, and the default link event happening - the same as returning false
. 函数将停止添加#,并发生默认的链接事件-与返回
false
相同。
HTML 的HTML
I've added the page1.php
links to the href
- so if Javascript is disabled for whatever reason, you'll still be able to navigate to that page. 我已经添加了
page1.php
链接到href
-所以,如果JavaScript是出于某种原因被禁用,你仍然可以导航到该页面。
<ul id="nav" class="nav" style="font-size:12px;">
<li><a href="page1.php" id="m_blink">tab1</a></li>
<li><a href="page2.php" id="d_blink">tab2</a></li>
<li><a href="page3.php" id="k_blink">tab3</a></li>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.