php - 从 div 打开超链接,并将其加载到同一个 div

标签 php jquery html css ajax

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 5 年前

目前有

$(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/

上一篇:php - 在页面加载时从目录加载随机图像(没有文件名列表)

下一篇:css - 可以使用 LESS 变量作为 @supports 规则吗?

相关文章:

javascript - 如何使用 JQuery 提取嵌套 HTML 中的文本?

PHP mySQL 时间戳需要返回日期为 00 :00:00

php - 使用 codeigniter php 插入数据后发送电子邮件

javascript - JQuery 添加选项以动态选择 7 种不同的选择

javascript - 在没有引用的情况下停止背景音频

javascript - 如何使 <select> 元素不选择默认选择的第一个选项?

php - 使用 PHP 生成的 Javascript 中的字符串中的转义引号字符

javascript - 根据id显示内容(来自数据库,而不是html id)

javascript - jQuery 获取 onChange 上的最后一个值

html - 如何使用 Beautiful Soup 按文本内容选择 div?