当iframe内容准备就绪时,从父页面访问iframe内容

时间:2021-12-17 21:13:08

I am doing file upload async with jquery/php using the target attribute of a form. The target is my iframe named "uploads"; To manage error in the php file, I return string like "ERR_TOO_LARGE_FILE" with an echo which will be inside the iframe body.

我正在使用表单的target属性与jquery / php进行文件上传异步。目标是我的iframe名为“uploads”;为了管理php文件中的错误,我返回一个像“ERR_TOO_LARGE_FILE”这样的字符串,其回显位于iframe体内。

The problem is I don't know how to detect when the iframe is ready and access its content to see if there's an error. I tried the following code but it didn't work:

问题是我不知道如何检测iframe何时准备就绪并访问其内容以查看是否存在错误。我尝试了以下代码,但它不起作用:

$("#uploads").on("load", function() {
    alert($(this).html());
});

an alert box did appear, but nothing inside. I also tried using the ready event instead but this time, nothing appears.

确实出现了警告框,但内部没有任何内容。我也试过使用ready事件,但这次没有出现。

Would like to know a way to achieve this or an alternate solution if you have a better one.

想知道如果你有一个更好的方法来实现这个或替代解决方案。

1 个解决方案

#1


4  

Because you're not allowed to modify the content of an iframe, at least you're not suppose to, you can't directly access it using jquery. Use .contents() & .find() to get the html from the iframe so you can get the info you need.

因为你不允许修改iframe的内容,至少你不应该这样做,你不能使用jquery直接访问它。使用.contents()和.find()从iframe中获取html,这样您就可以获得所需的信息。

So,

$("#uploads").on("load", function() {
    alert($(this).contents().find('html').html());
});

should return the contents of the html in the iframe. You can swap out the 'html' for a tag id if you know the specific element you're looking for.

应该返回iframe中html的内容。如果您知道要查找的特定元素,则可以将'​​html'替换为标记ID。

#1


4  

Because you're not allowed to modify the content of an iframe, at least you're not suppose to, you can't directly access it using jquery. Use .contents() & .find() to get the html from the iframe so you can get the info you need.

因为你不允许修改iframe的内容,至少你不应该这样做,你不能使用jquery直接访问它。使用.contents()和.find()从iframe中获取html,这样您就可以获得所需的信息。

So,

$("#uploads").on("load", function() {
    alert($(this).contents().find('html').html());
});

should return the contents of the html in the iframe. You can swap out the 'html' for a tag id if you know the specific element you're looking for.

应该返回iframe中html的内容。如果您知道要查找的特定元素,则可以将'​​html'替换为标记ID。