简体   繁体   English

div的jquery加载功能不起作用

[英]jquery load function for a div not working

I just want to alert 'OK' when a div with id="Box" loads. 我只想在加载id =“ Box”的div时提醒“确定”。 But load function is not working. 但是加载功能不起作用。 Here is my code in wordpress. 这是我在wordpress中的代码。 I have included the jquery library and other jquery is working fine. 我已经包括了jquery库,其他jquery都运行良好。

jQuery(function(){

jQuery( '#Box' ).load(function() {

alert('ok');
        });

    });

It does not work with DIV elements: 它不适用于DIV元素:

See: https://api.jquery.com/load-event/ 请参阅: https//api.jquery.com/load-event/

"The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object." “将加载事件和所有子元素完全加载后,会将其发送到元素。可以将该事件发送到与URL关联的任何元素:图像,脚本,框架,iframe和窗口对象。”

Question: 题:

I just want to alert 'OK' when a div with id="Box" loads. 我只想在加载id =“ Box”的div时提醒“确定”。 But load function is not working. 但是加载功能不起作用。


You are using .load() . 您正在使用.load()

Answer A : This method will not work with a div . 答:这个方法不适用于div

Answer B : This method was deprecated in jQuery 1.8. 答案B:此方法在jQuery 1.8中已弃用。 Stop using it! 停止使用它!

This method is a shortcut for .on( "load", handler ). 此方法是.on(“ load”,handler)的快捷方式。

The load event is sent to an element when it and all sub-elements have been completely loaded. 当加载事件和所有子元素都已完全加载时,会将加载事件发送到该元素。 This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object. 该事件可以发送到与URL关联的任何元素:图像,脚本,框架,iframe和window对象。

https://api.jquery.com/load-event/ https://api.jquery.com/load-event/


Possible solution : 可能的解决方案

Use the shorthand method .ajax() , .load() , .get to load content and trigger your stuff in the callback function or success handler. 使用简写方法.ajax() .load() load .load().get加载内容,并在回调函数或成功处理程序中触发内容。

.load() Load data from the server and place the returned HTML into the matched element. .load()从服务器加载数据,并将返回的HTML放入匹配的元素。

http://api.jquery.com/load/ http://api.jquery.com/load/

Example 1 例子1

Specify what to load in the first parameter and act in the callback function . 在第一个参数中指定要加载的内容并在回调函数中起作用 The content of test.html is loaded, after that it's inserted into #box and the alert is fired. 加载test.html的内容,然后将其插入#box并触发警报。

$( "#result" ).load( "ajax/test.html", function() {
    alert( "Load was performed." );
});

Example 2 例子2

Ajax Example with load alert: 带有负载警报的Ajax示例:

$.get("ajax/test.html", function(data) {
    $("#box").html(data);
    alert("Ok. Load.");
});

You are loading test.html the content is returned as data . 您正在加载test.html ,内容将作为data返回。 It is assigned as new html content to the dom element #box . 它被作为新的html内容分配给dom元素#box Then you fire the alert . 然后,您发出alert

Example 3 例子3

The alternative is using the .ajax method and define a success handler: 另一种方法是使用.ajax方法并定义一个success处理程序:

function getData() {
    $.ajax({
        url : 'test.html',
        type: 'GET',
        success : alert('Ok.Loaded')
    });
}

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

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