简体   繁体   English

页面加载完成后,如何在按下按钮时将内容加载到div中

[英]How to Load content into div upon button press, long after page finishes loading

I have searched the web and forums high and low and have not found a solution that I feel accurately addresses it. 我在网上和论坛上搜索过很多东西,但找不到适合我的解决方案。 I hope someone reading this can help solve my issue. 希望有人阅读可以帮助解决我的问题。 I have a website that catalogs the concerts I have for various music groups. 我有一个网站,其中列出了我为各种音乐团体举办的音乐会的目录。 Initially, all the dates, venues, and such for a given year are visible... a year 'row'. 最初,可以看到给定年份的所有日期,地点等等。 Each date in the row is actually a link, as shown here: 该行中的每个日期实际上都是一个链接,如下所示:

<a name = "<?php echo $tag_no ?>" class="none" href="#<?php echo $tag_no ?>" title = "<?php echo $tooltip ?>" onClick="$('#<?php echo $tag_no ?>').load('/2.0/details.php3?event_no=<?php echo $tag_no ?>&amp;details=0&amp;PHPSESSID=<?php echo session_id() ?>');"><?php echo $concert_date_formatted ?> </a>

When the date is clicked, the .load() inserts the details for the concert into the div. 单击日期时,.load()将音乐会的详细信息插入div。 It actually replaces the year 'row' and inserts all the details in the div. 它实际上替换了年份“行”,并将所有详细信息插入到div中。 This has worked great for years. 多年来,这一直很好。 I have a little javascript routine which attaches to the div and lets the user toggle it up and down... 我有一个小javascript例程,它附加到div上,并允许用户上下切换...

    <script language="JavaScript" type="text/javascript">
// window shade
function change(id){ 
 ID = document.getElementById(id); 

 if(ID.style.display == "") 
      ID.style.display = "none"; 
 else 
      ID.style.display = ""; 
}
</script>

The issue I have is that I have added some content to the details that are inserted via the .load() that takes longer than I want to populate. 我遇到的问题是,我已将一些内容添加到通过.load()插入的详细信息中,这些内容花费的时间比我要填充的时间还要长。 This happens before the page completes loading so it delays the overall list of dates and concerts from completing. 这是在页面完成加载之前发生的,因此会延迟整个日期和音乐会列表的完成。

I need a way to make the content in the details load only after the user clicks the date. 我需要一种仅在用户单击日期后才使详细信息中的内容加载的方法。 I don't want the page to query the database and webserver until after the link is pressed. 在链接被按下之前,我不希望页面查询数据库和Web服务器。 I am using javascript and jquery in other areas. 我在其他领域使用javascript和jquery。

Thanks for any insight! 感谢您的见解!

Cheers 干杯

Assume you have a button (in your case it seems is a date field): 假设您有一个按钮(在您的情况下,它似乎是一个日期字段):

<button id="button" onclick="myFunction()">Click me</button>

You only want to load the content when the user clicks the button (or a date field). 您只想在用户单击按钮(或日期字段)时加载内容。 In this case, when the user clicks on it, the myFunction() will be executed. 在这种情况下,当用户单击它时,将执行myFunction()。

In your myFunction(), you could have an AJAX event to bring the data from the DB. 在myFunction()中,您可以有一个AJAX事件来从数据库中获取数据。

function myFunction() {

//ajax event to bring data from database

}

Since you are Using JQuery, you can also do the following: 由于您正在使用JQuery,因此您还可以执行以下操作:

$("#button").click(function(){
    //ajax event to bring data from database
});

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

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