我如何得到一个片段从一个未指定的html加载

How do I get a fragment to load from an unspecified html?

本文关键字:一个 未指定 html 加载 片段 何得      更新时间:2023-09-26

使菜单消失后,如果不列出特定的html,我就无法显示我的内容。

这是HTML:

<div id="header">
<h1>N300 Project Gallery</h1>
</div>
<div id="container">
<div id="utility">
<ul>
    <li><a href="about.html">About</a></li>
    <li><a href="projects.html">Projects</a></li>
</ul>
</div>
    <div id="index-content">?</div>
    <div id="footer">This is the footer</div>
</div>
下面是我的脚本:
$(document).ready(function() {
    $('#utility a').click(function(e){
        e.preventDefault();
        $('#utility').hide('normal',loadContent);
    });
    function loadContent() {
        $('#index-content').load(url+ '#content')
    }
});

这是我想移动的片段:

<div id="content">
    <p>Hello! My name is Brittany Shephard.</p>
</div>

请注意,如果您想要片段加载行为(+ ' #content'而不是+ '#content'),则需要在传递给.load()的URL中的#content之前添加额外的空间。

你的片段是在一个单独的文件,对吗?

假设你的片段从projects.html #content返回,尝试:

function loadContent() {
    $('#index-content').load($(this).attr('href')+ ' #content');
}

从JQuery文档的隐藏,this被设置为隐藏的DOM节点在回调。我假设你想从你隐藏的锚加载URL,所以你需要从该节点抓取href属性。

loadContent()中的url从何而来

如果您需要从e.target href属性传递它,为hide的回调创建一个匿名函数,并从url传递到loadContent。Somtehing:

$(document).ready(function() {
    $('#utility a').click(function(e){
        e.preventDefault();
        $('#utility').hide('normal', function() {
            loadContent($(e.target).attr('href'));
        });
    });
    function loadContent(url) {
        $('#index-content').load(url + ' #content');
    }
});