简体   繁体   English

从锚标记中的属性 href 加载特定 div 中的 html 文件

[英]load a html file in a particular div from attribute href in a anchor tag

Div in HTML file HTML 文件中的 Div

<div class="content">
  <p class="info">Please choose a category</p>
</div>

anchor tag with href attribute带有 href 属性的锚标记

<a class="menu__link" href="{% url 'Profile' %}">Profile</a>

My javascript function我的 javascript 函数

 var gridWrapper = document.querySelector('.content');

            function loadDummyData(ev, itemName) {
                ev.preventDefault();

                closeMenu();

                gridWrapper.innerHTML = '';
                classie.add(gridWrapper, 'content--loading');
                setTimeout(function() {
                    classie.remove(gridWrapper, 'content--loading');
                    gridWrapper.innerHTML = $(".menu__link--current").attr("href")
                    console.log($(".menu__link--current").attr("href"))
                }, 500);
            }

Output in console is showing the path of the url控制台中的输出显示了 url 的路径

/Accounts/profile

If the ev.preventDefault();如果ev.preventDefault(); is removed, and commenting the line gridWrapper.innerHTML = $(".menu__link--current").attr("href") , the html page gets loaded but not in the particular content div.被删除,并评论了gridWrapper.innerHTML = $(".menu__link--current").attr("href") ,html 页面被加载,但不在特定的内容 div 中。

The problem is i want to load that particular html page with href attribute in content div.问题是我想在内容 div 中加载带有 href 属性的特定 html 页面。 Any Help will be much appreciated.任何帮助都感激不尽。 Thank You.谢谢你。

You cant load external html with InnerHTML .您不能使用InnerHTML加载外部 html。 InnerHTMl is used to insert the HTML elements into the document or targeted div. InnerHTMl用于将 HTML 元素插入到文档或目标 div 中。 To load an external Html file, you have to use .load要加载外部 Html 文件,您必须使用.load

var gridWrapper = document.querySelector('.content');
function loadDummyData(ev, itemName) {
    ev.preventDefault();

    closeMenu();

    gridWrapper.innerHTML = '';
    classie.add(gridWrapper, 'content--loading');
    setTimeout(function() {
      classie.remove(gridWrapper, 'content--loading'); 

      let path = $(".menu__link").attr("href") //Fetch path to the file
      console.log($(".menu__link").attr("href")) //Path has to be /file/filename.html
      $('.content').load(path); //========> use the path in the load method inside content div.
    }, 500);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM