<分区>
目前有
$(document).ready(function() {
// Check for hash value in URL
var hash = window.location.hash.substr(1);
var href = $('#menu a, #name a, #contact_us a, #item a, #text a').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-4)){
var toLoad = hash+'.php #pagearea';
$('#pagearea').load(toLoad)
}
});
$('#menu a, #name a, #contact_us a, #item a, #text a').click(function(e){
if($(e.target).is('foo')){
e.preventDefault();
window.location.href = "/current/forum";
return;
}
var toLoad = $(this).attr('href');
$('#pagearea').hide('fast',loadContent);
$('#load').remove();
$('#wrapper').append('<span id="load">LOADING...</span>');
$('#load').fadeIn('normal');
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
function loadContent() {
$('#pagearea').load(toLoad,'',showNewContent())
}
function showNewContent() {
$('#pagearea').show('normal',hideLoader());
}
function hideLoader() {
$('#load').fadeOut('normal');
}
return false;
});
});
效果很好。除非遵循来自#pagearea 内部的链接。它只转到 php 页面。我找到了以下主题
答案:更新 JQuery。使用的是过时的版本。问题已自行修复。
最佳答案
以下代码在 php 和 html 这两个文件中都可以正常工作
index.html:
<div id="menu">
<a href="default.php" class="menu" data-url="index">Home</a>|
<a href="about.php" class="menu" data-url="about">About</a>|
<a href="picture.php" class="menu" data-url="picture">Pictures</a>|
<a href="videos.php" class="menu" data-url="videos">Videos</a>
</div>
脚本.js:
$("document").ready(function() {
loadPage("default.html");
$(".menu").on("click", function(e) {
e.preventDefault();
var fullPath = $(this).attr("href");
loadPage(fullPath);
});
function loadPage(fullPath){
$("#pagearea").load(fullPath, function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
}
});
关于php - 从 div 打开超链接,并将其加载到同一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48359119/
相关文章:
javascript - 如何使用 JQuery 提取嵌套 HTML 中的文本?
PHP mySQL 时间戳需要返回日期为 00 :00:00
php - 使用 codeigniter php 插入数据后发送电子邮件
javascript - JQuery 添加选项以动态选择 7 种不同的选择
javascript - 如何使 <select> 元素不选择默认选择的第一个选项?
php - 使用 PHP 生成的 Javascript 中的字符串中的转义引号字符
javascript - 根据id显示内容(来自数据库,而不是html id)