简体   繁体   English

当div加载内容时如何发出警报

[英]How to alert something when a div loaded with content

I have aa page that contains search toolkit. 我有一个包含搜索工具包的页面。 So when the search button is clicked, results will be fetched by a ajax from database and appended to a div. 因此,当单击搜索按钮时,结果将由ajax从数据库中获取并附加到div。

It also shows number of records retrieved from database! 它还显示了从数据库检索到的记录数! I want to be able to do something immediately after the counter shows up a value. 我希望能够在计数器显示值后立即进行操作。

I tried on click of the search button, but it only shows from the second click onward. 我尝试单击搜索按钮,但仅从第二次单击开始显示。 Because when first clicked the search button, the data were not even fetched from database. 因为第一次单击搜索按钮时,甚至没有从数据库中获取数据。 Also, after data loaded, when click again it actually shows number of previously fetched records. 同样,在加载数据后,再次单击时,它实际上会显示以前提取的记录数。

I thought, jquery's load() would suit better, but it doesn't too. 我以为,jQuery的load()会更好,但事实并非如此。 Below are my attempts that failed. 以下是我失败的尝试。 Can anyone suggest the best method please? 有人可以建议最好的方法吗?

//This shows number of items fetched from database //这显示了从数据库中获取的项目数

len=data.length;

$(".len").append("<span class='light_blue' id='found'>"+len+"</span>
<span class='rm2' id='tname'> Tutors found</span>");

//This is how the results (fetched data) are appended into a div to be shown in the search page! //这是将结果(获取的数据)附加到div中以显示在搜索页面中的方式!

$(".the-inner-return").append("

All I want to do is to alert the count (basically I want to replace the text for id='tname' with something else when the count is 0 and 1 and more then 1). 我要做的只是提醒计数(基本上,当计数为0和1且大于1时,我想用其他方式替换id ='tname'的文本)。

Failed attempts: 尝试失败:

$("#search").on("click",function()
{
    alert($("#found").text());//alerts the count 
});

$(".the-inner-return").on("load",function()
{
    alert($("#found").text());//alerts the count 
});
$(document).on("click", "#search",function(){
   alert($("#found").text());//alerts the count 
});


$(document).on( "load", ".the-inner-return", function() {
      alert($("#found").text()); //alerts the count 
});

You can trigger an alert each time the innerHTML for .len gets changed, for instance like this you get an alert whenever len>0 : 每次.len的innerHTML更改时,您都可以触发警报,例如,当len>0时,您会收到警报:

$('.len').bind("DOMSubtreeModified", function () {
    if (len > 0) {
        alert('changed');
    }
});

 len = 5; $('.len').bind("DOMSubtreeModified", function() { if (len > 0) { alert('>0'); } }); $(".len").append("<span class='light_blue' id='found'>" + len + "</span><span class='rm2' id='tname'> Tutors found</span>"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="len"></div> 

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

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