我想在 iFrame 中查找 HTML 内容。我已遵循此代码,但似乎不起作用:
$('#iviFrame1').contents().find('#iViTag1').html();
iFrame 完全加载后,我在 Firefox 控制台上执行此操作。
我还检查了我的 iFrame 和其中的内容,并发现了 id 为 iViTag1 的 div :
<iframe
id="iviFrame1"
name="iVi Dashboard"
longdesc="iVi - Your Personal Dashboard"
style="display:inline"
src=""
width="100%"
height="98%"
align="middle"
frameborder="0"
marginwidth="0"
marginheight="0"
scrolling="auto"></iframe>
在 Iframe 内:
<div id="iViTag1" style="display: none;"></div>
我在 Firefox 控制台上执行了 $('#iviFrame1').contents().find('#iViTag1').html();
,控制台输出 underfined
这是我尝试过的:
$('#iviFrame1').contents();
Object { length: 0, prevObject: {…}, context: HTMLDocument http://localhost:8080/WS_ivi/?p=8&o={7f377407-04cc-464e-ac78-17738486e94a}&v=1.7.5.8 }
$('#iviFrame2').contents().find('#iViTag1');
Object { length: 0, prevObject: {…}, context: HTMLDocument http://localhost:8080/WS_ivi/?p=8&o={7f377407-04cc-464e-ac78-17738486e94a}&v=1.7.5.8, selector: "#iViTag1" }
$("#iViTag1");
{…}
context: HTMLDocument http://localhost:8080/WS_ivi/?p=8&o={7f377407-04cc-464e-ac78-17738486e94a}&v=1.7.5.8
selector: "#iViTag1"
__proto__: Object { jquery: "2.1.4", constructor: n(), length: 0, … }
顺便说一下,我使用的是 Firefox Quantum 57、JQuery 2.1.4,而我的 iFrame SRC 来自不同的 IP。我使用了 localhost 和来自另一台服务器的 iFrame SRC。
各种方法都试过了,还是不行。如果我从检查员那里检查,我会看到 iViTag1 存在。
有什么建议吗?
最佳答案
最好的猜测是您没有使用加载事件处理程序来允许 iframe 页面在查找元素之前加载:
$(function()
$('#iviFrame1').on('load', function(){
var $iV = $(this).contents().find('#iViTag1');
console.log('iv len=', $iV.length)
});
});
关于javascript - JQuery 在 iFrame 中查找 HTML 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48414318/