我尝试使用 jQuery 操作 WordPress 内容编辑器,该编辑器在 iframe 内使用 TinyMCE。我似乎无法让以下代码按预期工作:
jQuery(document).ready(function($) {
$('#content_ifr').ready(function() {
$('#content_ifr').contents().find('body').css('background-color', '#f33');
});
无论我使用“.ready()”还是“.load()”,该事件都会在 TinyMCE 编辑器(iframe 的主体)之前触发完全完成加载。
但是,如果我在手动单击标题文本框以触发单击事件之前等待 iframe 加载,则以下代码将起作用:
jQuery(document).ready(function($) {
$('#title').click(function() {
$('#content_ifr').contents().find('body').css('background-color', '#f33');
});
我已经对 jQuery 和 iFrames 的主题进行了详尽的搜索,似乎它的成功或失败取决于情况。有些代码适用于某些人,但不适用于其他人。
有谁知道在 TinyMCE iframe 主体完全加载完成后触发第一个代码片段的方法吗?因为它只是 WordPress 内容编辑器,所以 iframe 内容位于同一域中。
我真正想做的是通过 .addClass() 将一个类添加到 iframe 中的 body 元素,
jQuery(document).ready(function($) {
$('#content_ifr').ready(function() {
$('#content_ifr').contents().find('body').addClass('ui-droppable');
});
以便我可以在以下教程中应用拖放代码:
http://skfox.com/2008/11/26/jquery-example-inserting-text-with-drag-n-drop/
Using jQuery I'm trying to manipulate the WordPress content editor, which uses TinyMCE inside of an iframe. I can't seem to get the following code to work as intended:
jQuery(document).ready(function($) {
$('#content_ifr').ready(function() {
$('#content_ifr').contents().find('body').css('background-color', '#f33');
});
Regardless of whether I use ".ready()" or ".load()", the event will fire before the TinyMCE editor (the body of the iframe) is completely finished loading.
However, the following code will work if I wait for the iframe to load before manually clicking the title textbox to fire the click event:
jQuery(document).ready(function($) {
$('#title').click(function() {
$('#content_ifr').contents().find('body').css('background-color', '#f33');
});
I've searched exhaustively on the subject of jQuery and iFrames, and it seems it's a hit or miss depending on the situation. Some code works for some people and not for others.
Does anyone know of a way to get the first code snippet to fire after the TinyMCE iframe body is completely finished loading? Because it's just the WordPress content editor, the iframe content is on the same domain.
What I really want to do is add a class to the body element in the iframe via .addClass()
jQuery(document).ready(function($) {
$('#content_ifr').ready(function() {
$('#content_ifr').contents().find('body').addClass('ui-droppable');
});
so that I can apply the drag n' drop code in the following tutorial:
http://skfox.com/2008/11/26/jquery-example-inserting-text-with-drag-n-drop/
发布评论
评论(3)
您可以使用
tiny_mce_before_init
挂钩在tinymce.init之前将类添加到主体中You can use the
tiny_mce_before_init
hook to add a class to the body before tinymce.init我使用此函数在 Wordpess 中访问或设计 iframe(在 tinymce 内):
该脚本被添加到 WordPress 仪表板脚本中,并在 TINYMCE iframe 加载时进行检测,然后对其进行操作。
I use this function to access or style iframe (inside tinymce) in Wordpess:
this script is added to wordpress dashboard scripts, and detects while TINYMCE iframe is loaded, then actions to it.
看看你的tinymce配置。您需要做的就是使用 "setup"-setting 并设置onInit-Handler 那里。这是一个例子:
Have a look at your tinymce configuration. All you need to so is to use the "setup"-setting and set the onInit-Handler there. Here is an example: