2017-03-07 77 views
0

嗨,大家好我想嘗試在div的onload事件上調用一個函數,但是我找不到任何可以幫助我在onload事件上調用函數的東西。 我確實嘗試過使用這個「oQuickReply.swap」來調用它,但它不起作用,因爲它只會交換位置。那麼你能告訴我如何在div上添加一個onload事件嗎? 我的問題是不同的,因爲我有一個函數,只能在循環中調用'post_id'值到函數onload事件我想要調用我的函數select_comment()這將使我從我的所有評論數據庫到頁面。讓我告訴你我的代碼,如果有什麼你不明白告訴我,我會詳細說明。如何使用jquery或javascript將onload事件添加到div?

 <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加載時。

+1

它會更好,如果你可以張貼一些代碼! –

+1

'div'元素沒有'load'事件。如果您向我們提供更多信息,我們可以幫助您實現您的實際最終目標。 –

+0

'$('。my-div')。load(function(){})'??? – Justinas

回答

0

嘗試使用這樣的:

<div id="id">Some content</div> 
<script type="text/javascript"> 
    oQuickReply.swap('id'); 
</script> 
+0

http://stackoverflow.com/questions/4057236/how-to-add-的onload事件對A-DIV元素 –

0

使用jQuery這樣的:

<script 
       src="https://code.jquery.com/jquery-3.1.1.min.js" 
       integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" 
       crossorigin="anonymous"></script> 
<script> 
     $('.when-element-with-this-class').ready(function() { 
      alert("ready!"); 
     }); 
</script> 

<div class="when-element-with-this-class"> 
when this div is ready, js procced function with alert command 
</div> 

https://jsfiddle.net/g7re6khu/

+0

什麼是 '誠信' 和 'crossorign'? @Paul –

+0

嘿,這兩個字段是在使用CDN時出於安全問題。當你使用CDN時,它們是核心。關於它的更多信息: CORS https://en.wikipedia.org/wiki/Cross-origin_resource_sharing CDN https://en.wikipedia.org/wiki/Content_delivery_network 希望我幫助:) – Paul

+0

這顯示警報,即使我刪除div ... – Alkart

相關問題