如何使用 jquery 或 javascript 将 onload 事件添加到 div?

新手上路,请多包涵

大家好,我想在 div 的 onload 事件上调用一个函数,但我找不到任何可以帮助我在 onload 事件上调用函数的东西。我确实尝试使用这个“oQuickReply.swap”来调用它,但它不起作用,因为那只会交换位置。那么你能告诉我如何在 div 上添加 onload 事件吗?我的问题是不同的,因为我有一个只能在循环中调用的函数,以将“post_id”值传递给函数,我想要的 onload 事件是调用我的函数 select_comment(),这将从我的数据库中获取所有评论到页面。让我向您展示我的代码,如果有什么您不明白的地方,请事先告诉我,我会详细说明。

      <script type="text/javascript">
 $(window).load(function(e){

    // grab the scroll amount and the window height
       loadmore();
       select_likes();

       select_share();
       // get_recieve_friend_requests();
       // get_sent_friend_requests();
    });

 function loadmore(){
          var lastID = $('.load-more').attr('lastID');
         // alert(lastID);

              jQuery.ajax({
                  type:'POST',

                  url:'<?php echo base_url("user/get_all_post"); ?>',
                   data: {id:  lastID },
                      dataType: 'json',

                  beforeSend:function(data){
                      $('.load-more').show();
                  },
                  success:function(data){

                         var ParsedObject = JSON.stringify(data);
                         var json = $.parseJSON(ParsedObject);

                         if (json=="") {
                          $("#bottom").append('<div class="btn btn-default col-md-6" >'+'No More Results'+'</div>');
                          $("#Load_more_data").hide()

                         }else{

                           $postID=json[json.length-1].id;

              $('.load-more').attr('lastID', $postID);

                $.each(json, function (key, data) {

   var post_id=data.id;
    var post_status=data.status;
     var status_image=data.status_image;
    var multimage=data.multimage;

                             if(!post_status=="" && !status_image==""){
                               $("#status_data").append('<div class="panel-footer" onload="select_comment('+post_id+');"><div class="row"><div class="col-md-12"><a href="#">13 people</a> like this</div></div><ul class="media-list"><li   class="media_comment"></li><li class="media"><div class="media-left media-top"><?php echo img($user_file_image); ?></div><div class="media-body"><div class="input-group"><form action="" id="form_content"><textarea name="textdata" id="content_comment" cols="25" rows="1"  class="form-control message"  placeholder="Whats on your mind ?"></textarea><button type="submit" id="comment_button" onclick="comment_here('+post_id+');">Comment</button></form></div></div></li></ul></div></div>');

                    });
                  }
              }
            });
          }
function select_comment(post_id)
{

  alert(post_id);

  var User_id = $('.id_data').attr('value');
 jQuery.ajax({
                  type:'POST',
                  url:'<?php echo base_url("user/select_comment"); ?>',
                  data: {Post_id:Post_id,User_id:User_id},
                  dataType: 'json',
                  success:function(data)
                  {
                    alert('you have like this');
                  }
          });

}

现在您看到这就是我想在加载该 div 时使用我的函数进行调用的方式。

原文由 Himanshu Goyal 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 457
1 个回答

请参考下面的代码。可能会帮助你

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<style>
#divstyle {
text-align: center;
background-color: cadetblue;
width: 550px;
height: 180px;
margin-left: 100px;
}
</style>
</head>
<body onload="myFunction()">
<div id="divstyle">
<h2>Welcome to the tutorial for onload event!</h2>
<hr />
<h3>The function will be executed once the page is fully loaded.</h3>
</div>
<script>
function myFunction() {
alert("Hello, User!");
}
</script>
</body>
</html>

原文由 Bhavesh Bhuva 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题