繁体   English   中英

如何仅在加载 div 时运行 function?

[英]How do I run a function only when a div gets loaded?

我只想在加载 div 时运行 function 。

当我加载页面时,会加载许多文件。 在列表的末尾,PHP 呼应了一个 div。 当显示这个时,jQuery 应该运行 function。

我可以通过点击事件来做到这一点,但我希望它能够自动工作,而无需按下按钮。

这是它通过点击的工作方式:

$("#PP_end_show").live("click",function(){
    $("#PP_head_bar").slideToggle("slow");
    $("#PP_info_file_wrap").slideUp("slow");
}); 

这是 PHP 回应的div

 <?php echo "<div id=\"PP_end_show\"></div>"; ?>

output 在 AJAX 调用后生成:

<form id="PP_search_input" method="post" name="search_ID" ONSubmit="xmlhttpPost('PP_search_stream_client.php', 'PP_search_input', 'PP_thumb_output', '<img src=\'images/wait.gif\'>');return false;  ">
        <input name="search_string" type="text" class="PP_input" id="search_string" value="<?php echo $_POST['search_string']; ?>"/>
        <button type="submit" class="PP_submit" id="search_submit"> search </button>

在生成的 output 的末尾,将打印特定的 div,并应触发新的 jQuery function。

这就是我解决这个问题的方法,假设我已经正确理解了提交表单 PP_search_input,返回所需的 html,然后应该执行 javascript 代码。

$('#PP_search_input').submit(function() {
    $.ajax({
         url: 'PP_search_stream_client.php',
         type: 'post',
         data: $(this).serialize(),
         success: function(html) {
             $(html).insertAfter('#whereToInsert') //change to however you want to insert the html
                    .find("#PP_head_bar").slideToggle("slow").end()
                    .find("#PP_info_file_wrap").slideUp("slow");                       
         }
        });
});

尝试将您的 javascript 代码放入生成的 ajax 代码中。

例如,如果您的 ajax 是从 php 代码生成的

<?php echo "<div id=\"PP_end_show\"></div>"; ?>

然后像这样尝试

<?php 
    echo "<div id=\"PP_end_show\"></div>"; 
    echo '$(function(){$("#PP_head_bar").slideToggle("slow");';
    echo '$("#PP_info_file_wrap").slideUp("slow");});'; 
?>

您可以使用livequery 插件

然后,您将按如下方式使用它:

$('#PP_end_show').livequery(function(){ 
  $("#PP_head_bar").slideToggle("slow");
  $("#PP_info_file_wrap").slideUp("slow");
});

function 中的代码将在选择器 ( #PP_end_show ) 中的元素添加到 DOM 时执行

为什么不将您的 javascript 与 DIV 代码一起回显?

您可以使用 PHP 来回显 Javascript ,如下所示:

在您的代码之后:

<?php echo "<div id=\"PP_end_show\"></div>"; ?>

写这个:

echo "<script type='text/javascript'>

$('#PP_head_bar').slideToggle('slow');
$('#PP_info_file_wrap').slideUp('slow');


";
echo "</script>";

这应该有效。

在您的 JS 中,将要执行的代码包装在 function 中。 IE

function showInfoBlock() {
    $("#PP_head_bar").slideToggle("slow");
    $("#PP_info_file_wrap").slideUp("slow");
}

在您的 PHP 中,写出 PP_end_show div 后调用 function 所需的 JS。 IE

<?php echo "<div id=\"PP_end_show\"></div><script>$(function() { showInfoBlock(); })();</script>"; ?>

这可以满足您的要求:

(function($){
   var checkForEndInterval = window.setInterval(function(){
       if ($('#PP_end_show').length) {
           // stop interval
           window.clearInterval(checkForEndInvertal);
           // call your code now
           $("#PP_head_bar").slideToggle("slow");
           $("#PP_info_file_wrap").slideUp("slow");
       }
   },200);
})(jQuery);

然而,这不是一个好主意,因为它很愚蠢,你所要求的暗示你不理解,但这就是你在这里的原因。 由于您已经知道何时调用 AJAX,因此请显式调用它并为其附加成功处理程序。

$.ajax({
    /* your other setup code here */
    success : function(data) {
         // run your code
    }
});

如果你没有像你说的那样做 AJAX,我会在你的 output 的末尾放置一个脚本标签,并让它调用你的代码作为最简单的方法。 即使如此,您也可以使用 jQuery.load 方法(不是事件),默认情况下会从您的响应中执行 JavaScript。

快乐编码。

保持 jquery 实时点击功能不变

& 在 php 中运行以下代码

<?php 
echo "<div id=\"PP_end_show\"></div>"; 
echo "$(function(){ $(\"#PP_end_show\").click(); });"; 
?>

将实时事件绑定到由文档触发的自定义事件:

$(document).trigger('echo');

$("#PP_end_show").live("echo", function(){
  $("#PP_head_bar").slideToggle("slow");
  $("#PP_info_file_wrap").slideUp("slow");
  });

参考

    $.ready(function(){
     $("#wrapper").add("div").attr('id','PP_end_show');

            $("#PP_head_bar").slideToggle("slow");
            $("#PP_info_file_wrap").slideUp("slow");    

});


<div id='wrapper'>

</div>  

Jason Brumwell输入正确答案,您也可以使用 JQuery 的$.when .when function。

http://api.jquery.com/jQuery.when/

如果您希望在所需的 div 加载后执行的操作绑定到单击事件,只需在 ajax 请求结束时触发单击,如下所示:

$.ajax({
  url:'whatever.php',
  type:'post', //guessing obviously
  success: function(data){
    $('#PP_end_show').click(); //etc
  }
});

这些家伙是对的,你在做这一切都是倒着的,伙计

它必须是那个确切的div吗? 在我看来,您应该在加载文档时执行 function 。 这样你就知道你所有的元素都被加载了。

$(function()
{
  // This function is executed when the DOM is ready, including that last div. 
  // Could be that images are still loading, but your code usually won't depend on that.
}

尝试这个,

if(window.isBodyLoaded){
    try{
        $("#PP_head_bar").slideToggle("slow");
        $("#PP_info_file_wrap").slideUp("slow");
    }catch(d){
        alert(d)
    }
}

使用回调 function。 它是一个 function,一旦你的事件被处理就会被触发。 所以对于 Jquery:

$("#PP_end_show").live("click",function(){

  $("#PP_head_bar").slideToggle("slow");
  $("#PP_info_file_wrap").slideUp("slow");

}, myCallback());

function myCallback(){
//Do what ever you want to happen after 
//the div creationin this function
}

我认为这应该可以解决问题

暂无
暂无

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

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