将焦点设置为嵌套iframe中的文本框

Set focus to a textbox in a nested iframe

本文关键字:文本 iframe 嵌套 焦点 设置      更新时间:2024-01-09

我在应用程序中使用TinyMce。因此,我有嵌套的body元素和iframe。一个来自我的html页面,一个来自TinyMce。在tinymce的正文中,我添加了带有许多文本框的div。他们的id是hello1、hello2等。

如何将焦点设置为"hello1"?

此代码:

$iframe.contents().find('body').find('hello1')

返回正确的文本框。

但是这个:

$iframe.contents().find('body').find('hello1').focus()

不起作用。如何解决这个问题?

我认为您需要同时关注元素和相关的iframe。实际上,每一帧都是一个不同的窗口,因此每个帧都可以有一个聚焦元素。因此,实际上将仅选择聚焦的CCD_ 2中的聚焦元件。

$iframe.focus().contents().find('#hello1').focus()

同样,正如其他人所说,您还需要#hello1,而不仅仅是hello1,来通过其id来选择元素。还要注意,您可以删除.find('body'),因为文档中该id旁边可能只有一个元素。

试试这个:

$iframe.contents().find('body').find('hello1').focus()->$iframe.contents().find('#hello1').focus()

在我看来,您的ID选择器中缺少#。同样孤独的一天是正确的,.find('body')是不需要的,因为.contents()已经包含了您的元素。