如何让 div 在点击时加载而不是在页面加载时加载?

发布于 2024-07-26 10:02:47 字数 401 浏览 8 评论 0原文

我目前正在建立一个新网站。 它使用一页并使用 jQuery 手风琴菜单来加载内容。 它的内容分为六个不同的 div,并且在访问页面后立即加载。 我希望它在单击链接时加载每个 div。 减少页面加载时间。

您可以在我正在进行的网站上看到这一点:http://is.gd/1p1Ys

因为我没有制作 jQuery 脚本,而且我不擅长 JavaScript/jQuery,所以我没有一个线索来做什么。 所以我想知道这里是否有人可以帮助我。

非常感谢,

瑞安

I am currently building an making my new website. It uses one page and uses a jQuery accordian menu to load the content. The content it split up in six different div's and all load as soon as the page is accessed. I want it to load each div when the link to it is clicked on. To decrease the page loading time.

You can see this in action on my work in progress site here: http://is.gd/1p1Ys

Since I didn't make the jQuery script and I am really bad at JavaScript/jQuery I don't have a clue what to do. So I am wondering if anybody here can help me out.

Thanks a ton,

Ryan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肩上的翅膀 2024-08-02 10:02:47

我同意 jquery-ui tabs 做这件事的其他答案,但是你可以很容易地破解你的 Accordian.js 文件...只需更改导航中的 href 以指向包含你想要的内容的外部 html 文件,即href="#lifestream" => href="lifestream.html",

然后在 Accorian.js 中:

$links.click(function () {
  var link = this,
  if(!link.href.match(/^#.*/)) { //if the href doesn't start with '#'
    loadDiv(link.href);
  } else {
    doRestOfFunction(); //everything your script is currently doing.
  }
});

function loadDiv(href) {
  $.get(href, function(html) {
   var newDiv = $(html)
   $(".panels").append(newDiv);
   newDiv.hide();
   doRestOfFunction(); //same as above
  }
}

I agree with the other answers that jquery-ui tabs does this exact thing, but you can hack your accordian.js file pretty easily... just change the hrefs in your navigation to point to external html files containing the content you want, i.e. href="#lifestream" => href="lifestream.html",

and then in accordian.js:

$links.click(function () {
  var link = this,
  if(!link.href.match(/^#.*/)) { //if the href doesn't start with '#'
    loadDiv(link.href);
  } else {
    doRestOfFunction(); //everything your script is currently doing.
  }
});

function loadDiv(href) {
  $.get(href, function(html) {
   var newDiv = $(html)
   $(".panels").append(newDiv);
   newDiv.hide();
   doRestOfFunction(); //same as above
  }
}
丢了幸福的猪 2024-08-02 10:02:47

根据您的描述,听起来这些 div 在单击顶级菜单项之前不会显示。 如果是这种情况,您可以使用 jquery 在打开时将子菜单 div 插入到 DOM 中。

您可以在 w3schools 找到有关 Javascript/DOM 操作的不错的教程。

From your description, it sounds like these divs are not displayed until the top-level menu item is clicked. If this is the case, you can use jquery to insert the sub-menu divs into the DOM when it is opened.

You can find a decent tutorial on Javascript/DOM Manipulation at w3schools.

A君 2024-08-02 10:02:47

我不确定手风琴控件是您想要的 - 查看 JQuery 选项卡 - 您可以通过 Ajax 加载内容,这将阻止它们一次加载全部内容。 关于如何执行此操作的说明位于此处

I'm not sure the accordion control is what you are after - check out JQuery tabs - you can load the content via Ajax which will prevent them loading all at once. An explanation of how to do this is here

空宴 2024-08-02 10:02:47

如果您想动态加载内容,我建议您查看 jquery-ui 选项卡。 目前我还没有看到可以处理它的手风琴插件,这是有道理的,因为手风琴需要知道内容的高度,而且如果当您单击面板时您必须等待动态内容,它会显得生涩。 Tabs 是您前进的最佳且最合适的方式。

I would suggest looking at jquery-ui tabs if you want to load the content in dynamically. Currently I have not seen an accordian plugin that will handle it, which makes sense as an accordian needs to know the height of the content, also if when you clicked a panel you had to wait for dynamic content it would appear jerky. Tabs is your best and most appropriate way to move forward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文