最后加载特定的 div

发布于 2024-12-09 00:53:14 字数 83 浏览 0 评论 0原文

我正在尝试制作一个页面,其中某个 div(包含大量 php、html 和 javascript 内容)在页面的其余部分之后加载。如果可以的话,这可能吗?

I'm trying to make a page where a certain div (with lots of php, html and javascript content) loads after the rest of the page. Is this possible if so how?

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

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

发布评论

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

评论(5

荒芜了季节 2024-12-16 00:53:14

您可以将此 div 应用于隐藏样式,然后使用 javascript 来显示它:

$(function() {
    $('#someDiv').show();
});

但是如果您想避免最初加载它,您可以使用 AJAX:

$(function() {
    // <div id="container"></div> will be an empty div
    $('#container').load('/script');
});

另请注意,如果您希望在加载所有其他内容(包括图形)后进行此加载,您可以使用 $(window).load 而不是 $(document).ready

$(window).load(function () {
    ...
});

You could apply this div a hidden style and then use javascript to show it:

$(function() {
    $('#someDiv').show();
});

But if you want to avoid loading it initially you could use AJAX:

$(function() {
    // <div id="container"></div> will be an empty div
    $('#container').load('/script');
});

Also note that if you want this loading to happen once all other content is loaded including graphics you could use the $(window).load instead of $(document).ready:

$(window).load(function () {
    ...
});
书信已泛黄 2024-12-16 00:53:14

我有一个类似的问题,需要在创建某个 div 后填写数据。
我对此堆栈使用 .ejs 文件。

我所做的就是将所有代码拉到 .ejs 页面并按照我需要的方式对其进行排序。

例如。

<script type="text/javascript">
    jQuery making the divs
</script>
<% Pull Data and put it in the divs above %>

I had a similar Issue, needed the data to be filled in after a certain div was created.
I use .ejs files for this stack.

What I did was Pull all the code to the .ejs page Ordering it in the way I needed.

For Ex.

<script type="text/javascript">
    jQuery making the divs
</script>
<% Pull Data and put it in the divs above %>
别低头,皇冠会掉 2024-12-16 00:53:14

查看 jQuery 的 $.ajax 和相关函数。我想您会发现它们正是您正在寻找的东西。一个简单的例子:

<script type="text/javascript">
$(function() { // called when page is done loading; you can have lots of these
  $.ajax({
    url: 'other-content.php',
    success: function(data) { $('#load-me-later').html(data); }
  });
});
</script>
<div id="load-me-later"></div>

look in to jQuery's $.ajax, and related functions. I think you'll find they're exactly what you're looking for. a simple example:

<script type="text/javascript">
$(function() { // called when page is done loading; you can have lots of these
  $.ajax({
    url: 'other-content.php',
    success: function(data) { $('#load-me-later').html(data); }
  });
});
</script>
<div id="load-me-later"></div>
天荒地未老 2024-12-16 00:53:14

一定

<div id="the_div">some text or whatever</div>

要有

 #the_div { display:none; }

css中

。然后

 $(document).ready(function() {
     $("#the_div").show();
 });

The certain

<div id="the_div">some text or whatever</div>

must have

 #the_div { display:none; }

in css.

An then

 $(document).ready(function() {
     $("#the_div").show();
 });
清泪尽 2024-12-16 00:53:14

有几种方法可以做到这一点。其一,您可以将该 div 放在页面的末尾,并在其前面使用 Flux() ;但这不是很可靠,而且很难获得正确的位置和喜好。更好的方法是 ajax:

$.ajax({
  url: 'myurl.php',
  data: someDataMaybe,
  success: function(html) {
    $('#mydiv').html(html); // document.getElementById('mydiv').innerHTML = html would be a thousand times faster, but I've been told jquery people like this more
  }
});

这只会在页面加载后执行,然后将内容加载到 div 中。当然,它假设 myurl.php 仅输出应该位于所述 div 中的内容。

假设你有这个页面:

<div id="page">
  <div id="content">
    <!-- lotsa stuff in here -->
  </div>
  <div id="mydiv"></div>
</div>
<script type="text/javascript">
// the stuff above ^
</script>

myurl.php 输出这个:

Some PHP-generated stuff

这将导致:

<div id="mydiv">Some PHP-generated stuff</div>

There are several ways to do this. For one, you could put that div at the end of the page and flush() just before it; but this isn't very reliable, and it's pretty darned hard to get position and the likes right. A better way would be ajax:

$.ajax({
  url: 'myurl.php',
  data: someDataMaybe,
  success: function(html) {
    $('#mydiv').html(html); // document.getElementById('mydiv').innerHTML = html would be a thousand times faster, but I've been told jquery people like this more
  }
});

this will only execute after page load and then load the contents into the div. It of course assumes myurl.php outputs only content that should be in said div.

So say you have this page:

<div id="page">
  <div id="content">
    <!-- lotsa stuff in here -->
  </div>
  <div id="mydiv"></div>
</div>
<script type="text/javascript">
// the stuff above ^
</script>

And myurl.php outputs this:

Some PHP-generated stuff

which would result in:

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