【问题标题】:jQuery.load function issue loading unspecified elements more than oncejQuery.load 函数问题多次加载未指定的元素
【发布时间】:2020-10-04 05:05:34
【问题描述】:

如果我说错/问错了,我很抱歉,我是新手,正在学习,但需要帮助。

我的主 index.html 设置了 jquery mini 和我的 app.js 链接到它。我正在使用

let about = document.getElementById('aboutme');
about.onclick = function go() {
    $("#loadtarget").load("about.html #loadtarget");
}
let home = document.getElementById('home');
home.onclick = function go() {
    $("#loadtarget").load("index.html #loadtarget");
}

在我的 app.js 中,这样当有人点击它时,它会加载 about.html #loadtarget 元素,当我从家到 about me 时,它​​工作正常,但是,当我从 about me 回到家时 我的导航栏重复了,出现了两次……现在在我的 index.html 中,我的导航栏不在我的加载目标中

<main class="main" id="loadtarget">

在浏览器中调试时我也看到了:

Uncaught SyntaxError: Identifier 'navbar' has already been declared
    at b (jquery-3.5.1.min.js:2)
    at Function.globalEval (jquery-3.5.1.min.js:2)
    at Object.dataFilter (jquery-3.5.1.min.js:2)
    at jquery-3.5.1.min.js:2
    at l (jquery-3.5.1.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.min.js:2)
    at Object.send (jquery-3.5.1.min.js:2)
    at Function.ajax (jquery-3.5.1.min.js:2)
    at Function.S._evalUrl (jquery-3.5.1.min.js:2)
    at Pe (jquery-3.5.1.min.js:2)

关于如何防止我的导航栏在我回到家时加载两次的任何想法? 另外,我不知道这是否有帮助:导航栏是我自己设计的导航栏,不使用 jquery 的导航栏 api,我在添加 jquery-mini 文件之前在我的网站上拥有它。

作为旁注:当我从家里去关于我时,我知道我的 app.js 会执行,因为我的页脚在那里,这是 JS 添加的一个元素来设置年份和版权,但还有一些其他功能(我的一些 onclick函数)停止工作,这也让我感到困惑,任何帮助将不胜感激。

navbar onclick 函数运行正常,但我的网站上有一些 JS 示例,用户可以在其中单击主元素中的 ap 元素,它会将 innerHTML 设置为不同的东西,展示 JS 技巧,这就是停止工作的函数

再次抱歉,如果我的问题表述有误,我还在学习。

【问题讨论】:

    标签: javascript html jquery css ajax


    【解决方案1】:

    我通过更改修复了导航栏问题

    let about = document.getElementById('aboutme');
    about.onclick = function go() {
        $("#loadtarget").load("about.html #loadtarget");
    }
    let home = document.getElementById('home');
    home.onclick = function go() {
        $("#loadtarget").load("index.html #loadtarget");
    }
    

    let about = document.getElementById('aboutme');
    about.onclick = function goAbout() {
        $("#loadtarget").load("about.html #loadtarget");
    }
    let home = document.getElementById('home');
    home.onclick = function goHome() {
        $("#loadtarget").load("index.html #loadtarget");
    }
    

    只是更改了函数名称,它还摆脱了未捕获的语法错误... 但是,我的脚本无法正常运行(我的一些 onclick 功能仍然无法正常工作,有什么想法吗?

    navbar onclick 函数运行正常,但我的网站上有一些 JS 示例,用户可以在其中单击主元素中的 ap 元素,它会将 innerHTML 设置为不同的东西,展示 JS 技巧,这就是停止工作的函数

    【讨论】:

      猜你喜欢
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      相关资源
      最近更新 更多