jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 如何使用哈希标记Href刷新页面大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个href为“/ news#1930”的链接.当我在/ news页面上点击此链接时,它不刷新页面(它只是将#1930添加到URL的末尾).我希望它刷新页面(我使用#tag后面的信息和一些 jquery来加载相应的iframe中的特定文章,当它不刷新时它不会加载新文章).关于如何强制刷新的任何想法?

我目前的代码是:

@H_197_9@$(document).ready(function() { var $news_story = $(window.LOCATIOn.hash.sub@R_675_10495@ng(1)),$news_number = $news_story.SELEctor,$news_url = 'http://www.synergy-PR.com/news/mediacenter/index.html?view=article&CustomerID=41b1if9-a17d4va5sf7of15e3kb0pa3kd2y-99d03n8sq4ad2xk8h47j00r98el5pf&ClientArticlEID=' + $news_number,//url of specific article $news_url_default = 'http://www.synergy-PR.com/news/mediacenter/index.html?CustomerID=41b1if9-a1hd4xa52f78f1ke3bb0oa3ld2k-90208c8g64x02nh8wf7ad0m181p5su'; //url of general news page if ($news_number.length>0) { //if their is a # with a number,this means it is a sidebar link and should load that specific article $('#news-Feed').attr('src',$news_url); } else { //if not,load the main news page $('#news-Feed').attr('src',$news_url_default); } });

解决方法

哈希意味着不会导致页面重新加载.如果您正在使用jQuery加载该文章,您可能需要执行history.go(0)或window.LOCATIOn.reload,然后使用jQuery查找散列标记和进程(在刷新页面的加载事件上).

@H_197_9@$(document).ready(function(){ loadArticle = function(articlEID){ // code to query/load the article }; if (window.LOCATIOn.hash) articleLoad(window.LOCATIOn.hash); $('.articleLink').click(function(){ window.LOCATIOn.hash = $(this).attr('rel'); window.LOCATIOn.reload(); }); });

编辑我假设您正在使用哈希路线,因为像facebook这样的网站也会这样做 – 尽管您使用ajax进行无缝集成,但书签能力和索引仍然存在.

编辑2以下是我提出来展示我所指的内容.这将加载已通过URL传递的哈希值,并处理页面中新文章的任何新点击.

@H_197_9@<html> <head> <title>Article Viewer</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ // loader function to serve up the correct article (pushes the article number to an iFrame // so it can remotely load the article within the page loadArticle = function(articlEID){ // set the hash (incase it's called from a link instead of a page load) window.LOCATIOn.hash = articlEID; // change the iFrame src to the actual path fo the article page (customize this to fit your needs) $('#article-frame').attr('src','view_article.PHP?article='+articlEID); }; // check for a hash (#????) tag in the URL and load it (to allow for bookmarking) if (window.LOCATIOn.hash) loadArticle(window.LOCATIOn.hash.sub@R_675_10495@ng(1)); // attack a click event to all the links that are related to loading an article $('.article-link').click(function(){ // grab raw article id from rel tag var articlEID = $(this).attr('rel'); // shove loading it off to the "loader" loadArticle(articlEID); // cancel the navigation of the link return false; }); }); </script> </head> <body> <ul> <?PHP for ($a = 1; $a < 10; $a++) echo "<li><a href=\"view_article.PHP?article={$a}\" rel=\"{$a}\" class=\"article-link\">View Article {$a}</a></li>\r\n "; ?> </ul> <iframe id="article-frame" src="view_article.PHP?article=0" width="640" height="480"> This page uses frames,unfortunately your browser does not support them. </iframe> </body> </html>

大佬总结

以上是大佬教程为你收集整理的jquery – 如何使用哈希标记Href刷新页面全部内容,希望文章能够帮你解决jquery – 如何使用哈希标记Href刷新页面所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。