0
我试图从http://html5.gingerhost.com/重新创建演示时遇到一些问题。我会尝试描述尽可能明确的情况:jQuery pushState加载内容,不同的URL
在我的页面上有< A HREF =“指数”>点击时,它触发其中:
- 一个pushState的事件改变地址栏浏览器进入mysite.tld /指数
- 加载一个随机的网址,并把它而不是当前<的HREF =“指数”>
当用户点击一个个电话的getJSON的e加载的随机url,pushState事件不会触发,浏览器会跟随随机url ...这不是我想要的。我想继续触发pushState事件并继续加载随机地址。
事情是这样的:
- 单击索引
- LOAD索引2和REPLACE INDEX
- CLICK索引2 (这里打破它加载实页。)
- LOAD INDEX3和REPLACE INDEX2 (这里我想到)
- CLICK索引2 (这里打破它加载实页。)
- LOAD索引2和REPLACE INDEX
我道歉,如果我不清楚。我对我的jQuery技能不是很有信心。
你有什么建议吗?
这里是源代码:
<!DOCTYPE html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('a').click(function(e) {
href = $(this).attr("href");
loadContent(href);
history.pushState('', 'New URL: '+href, href);
e.preventDefault();
});
});
function loadContent(url){
$.getJSON("load.php", {pid: url}, function(json) {
$.each(json, function(key, value){
$(key).html(value);
});
});
}
</script>
</head>
<body>
<div>
<a href="index">Go to: index</a>
</div>
<p></p>
</body>
</html>
load.php
<?php $i = rand(0,10); ?>
{
"div":"<a href='<?php echo $_GET['pid'].$i; ?>'>Go to: <?php echo $_GET['pid'].$i; ?></a>",
"p":"This is the page for <?php echo $_GET['pid']; ?>"
}
谢谢!这工作! – 2013-04-22 12:22:13