[英]jQuery error when using $window.load
I have the following code that works inside a document ready event: 我有以下代码在文档就绪事件中工作:
jQuery(document).ready(function($) {
$('tr[data-name="background_colour"] input.wp-color-picker').each(function() {
//this works
$(this).closest('tr').siblings('tr[data-type="wysiwyg"]').css('background-color', this.value);
//this doesn't because frames are not loaded
$(this).closest('tr').siblings('tr[data-type="wysiwyg"]').find('iframe').contents().find('body').css('background-color', this.value);
});
});
The background color of the tr
is changed successfully but not the body
of the iframe
because all iframes are dynamically created and they are not fully loaded yet when document is ready. tr
的背景颜色已成功更改,但不是iframe
的body
,因为所有iframe都是动态创建的,并且在文档准备好时它们尚未完全加载。
So I tried using window.load instead as follows: 所以我尝试使用window.load,如下所示:
jQuery(window).load(function($) {
$('tr[data-name="background_colour"] input.wp-color-picker').each(function() {
//this works
$(this).closest('tr').siblings('tr[data-type="wysiwyg"]').css('background-color', this.value);
//this doesn't because frames are not loaded
$(this).closest('tr').siblings('tr[data-type="wysiwyg"]').find('iframe').contents().find('body').css('background-color', this.value);
});
});
But I got Uncaught TypeError: object is not a function
on the line below when I did that: 但是我得到了
Uncaught TypeError: object is not a function
当我这样做时, Uncaught TypeError: object is not a function
下面一行Uncaught TypeError: object is not a function
:
$('tr[data-name="background_colour"] input.wp-color-picker').each(function() {
Got this to work thanks to Guffa's answer. 感谢Guffa的回答让这个工作。 Here is the correct code which might be helpful for other people:
以下是可能对其他人有用的正确代码:
jQuery(window).load(function() {
jQuery(function($){
$('tr[data-name="background_colour"] input.wp-color-picker').each(function() {
$(this).closest('tr').siblings('tr[data-type="wysiwyg"]').css('background-color', this.value);
$(this).closest('tr').siblings('tr[data-type="wysiwyg"]').find('iframe').contents().find('body').css('background-color', this.value);
});
});
});
The ready
event handler is called with the jQuery object as parameter, but the load
event handler isn't. 使用jQuery对象作为参数调用
ready
事件处理程序,但不执行load
事件处理程序。
As you have $
as parameter for the load
event handler, it will contain the event object, not the jQuery object. 因为你有
$
作为load
事件处理程序的参数,它将包含事件对象,而不是jQuery对象。 When you try to use the event object as if it is the jQuery object, you will get an error. 当您尝试将事件对象用作jQuery对象时,您将收到错误。
Remove the parameter from the load
event handler, so that you can access the $
variable from the global scope: 从
load
事件处理程序中删除参数,以便可以从全局范围访问$
变量:
jQuery(window).load(function() {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.