javascript - 在 jquery 中选择另一个文件中的元素
问题描述
如果打扰到您,开发人员很抱歉。所以让我们进入这个问题。伙计们也请考虑我正在使用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 加载到我的代码中?
解决方案
.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
})
推荐阅读
- akeneo - 不符合 RFC 的 ajax 请求
- syndesis - Unable to build image using the OpenShift build service: Can't instantiate binary build, due to error reading/writing stream
- jquery - 如何从多个列表项中的元素中获取值
- ios - 我们可以在Windows机器中将iOS移动自动化的代码写入eclipse+appium而不需要Xcode吗
- java - 我无法通过我的应用程序从 firebase 检索动态链接,因为 pendingdynamiclinkdata 为空
- php - 如何从 url 获取数据到数组?
- spring-boot - 在 Spring Boot Java 项目中,Google Contact V3 使用哪个 Maven 依赖项?
- python - 是否可以使用appium删除文件?
- c++ - 将 QuaZip 添加到项目中
- powershell - 显示共享上每个文件夹的已用空间(无 WMI 等)