【问题标题】:select an element in jquery which is in another file在 jquery 中选择另一个文件中的元素
【发布时间】:2020-07-10 03:24:21
【问题描述】:

如果打扰到您,开发人员很抱歉。所以让我们进入这个问题。 伙计们也请考虑我正在使用nodejs和electronjs。 关于目录的简单表示

main-doc
  |---src
       |---Html
       |     |---index.html
       |     |---navbar.html
       |
       |---svg
       |    |---home.svg   
       |
       |---js
            |---jquery-file.js

jquery 文件被导入到 index.html 以重用导航栏。 所以我的jQuery代码,

$(".navbar-container").load("navbar.html")
console.log($(".icon-container")) // shows no element in console
$(".icon-container").load("../svg/home.svg") // icon-container is in navbar.html

所以我认为由于图标容器位于 navbar.html 中,因此在第二行时未正确加载 执行。在这种情况下,如何使用 jquery 将此 svg 加载到我的代码中?

【问题讨论】:

    标签: javascript jquery html node.js


    【解决方案1】:

    .load本质上是async。你可以看on complete function。并在那里加载/工作。

    示例:

    $( "#result" ).load( "ajax/test.html", function() {
      alert( "Load was performed." );
    });
    

    你的情况:

    $(".navbar-container").load("navbar.html", () => {
     console.log($(".icon-container")) // shows no element in console
     $(".icon-container").load("../svg/home.svg") // icon-container is in 
     navbar.html
    })
    

    阅读文档:https://api.jquery.com/load/#load-url-data-complete

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多