te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - jquery .load isn't checking if my element is loaded - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery .load isn't checking if my element is loaded - Stack Overflow

programmeradmin3浏览0评论

I have this code

<script type="text/javascript">
$("#main_photo_display").load(function(){
    alert("loaded");
});
</script>

<div id="main_photo_display"></div>

I need it to do something once that div has loaded. Currently it does nothing. When I substitute window for "#main_photo_display" it works. I have googled and I keep ing across .load as how to check if a page element has been loaded.

I have this code

<script type="text/javascript">
$("#main_photo_display").load(function(){
    alert("loaded");
});
</script>

<div id="main_photo_display"></div>

I need it to do something once that div has loaded. Currently it does nothing. When I substitute window for "#main_photo_display" it works. I have googled and I keep ing across .load as how to check if a page element has been loaded.

Share Improve this question asked Dec 21, 2011 at 0:25 ChrisChris 1551 gold badge1 silver badge6 bronze badges 1
  • What are you waiting to load? An image? An AJAX request? – Jasper Commented Dec 21, 2011 at 0:34
Add a ment  | 

6 Answers 6

Reset to default 3

The load event is sent to an element when it and all sub-elements have been pletely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

Source: http://api.jquery./load-event/

Further down on the same page they state:

It doesn't correctly bubble up the DOM tree

So you can't delegate this event, the event handler must be attached to the element on which the load event fires.

Or you can run the script after the DOM is ready like so:

<script type="text/javascript">
$(document).ready(function() {
    $("#main_photo_display").load(function(){
        alert("loaded");
    });
});
</script>

<div id="main_photo_display"></div>

Sorry I think I read it wrong :) You need this:

<script type="text/javascript">
    $(document).ready(function() {
        alert('loaded');
    });
</script>

A plain div does not have a load event except when you are loading content into it with ajax (which I don't think is what you are doing here). If your code is physically located after the div in your page, then the div will be available and ready for your code to operate on it (you don't have to check anything).

If your code is located before the div in the page, then you can use jQuery's .ready() method to know when it is safe to access the div:

<script type="text/javascript">
    $(document).ready(function() {
        // safe to access $("#main_photo_display") here
    });
</script>

<div id="main_photo_display"></div>    

I don't think a DIV fires a loaded event. If there was a blank.gif image within the DIV, you could attach the $.load() function to that.

<div id="main_photo_display">
  ..... Other Content .....
  <img class="loadcheck" src="blank.gif" width="0" height="0" />
</div>

<script type="text/javascript">
$(document).ready(function(){

  $("#main_photo_display img.loadcheck").load(function(){
    alert("loaded");
  });

});
</script>

You can't do that: load events are not fired on just any HTML element, only on those that require loading an external resource.

The best way to ensure the element is loaded is to put the script tag after it in the markup.

<div id="main_photo_display"></div>

<script type="text/javascript">
    alert("loaded");
</script>

The Javascript will not be run before the div is parsed.

I have a sort of workaround, and it is sloppy (please ment out if you have notes).

It is useful when you have a javascript out of your control which appends elements to your dom on a page load.

$(function () {

    var counter = 0;

    var intervalId = setInterval(function () {
        $(document).mouseover()
    }, 105);

    var unbind = function () {

        $(document).off('mousemove', '#label');

        $(document).off('mouseover');

        window.clearInterval(intervalId);

    };

    $(document).mouseover(function () {

        $('#label').trigger('mousemove');

        counter++;

        if (jivositecounter > 200) unbind();

    });

    $(document).on('mousemove', '#label', function () {
        console.log(counter);
        ...doing our stuff when #label appears
        unbind();
    });

});
发布评论

评论列表(0)

  1. 暂无评论