[英]JQuery and frames - $(document).ready doesn't work
我有一個頁面,在js和jQuery中有一些代碼,它運行得很好。 但不幸的是,我的所有網站都非常陳舊,並使用框架。 因此,當我將頁面加載到框架內時, $(document).ready()
不會啟動。
我的框架集看起來像:
<frameset rows="79,*" frameBorder="1" frameSpacing="1" bordercolor="#5996BF" noresize>
<frame name="header" src="Operations.aspx?main='Info.aspx'" marginwidth="0" marginheight="0" scrolling="no" noresize frameborder="0">
<frame name="main" src="Info.aspx" marginwidth="0" marginheight="0" scrolling="auto" noresize frameborder="0">
</frameset>
我的頁面被加載到main
框架中。 我該怎么辦?
我試過另一條評論中提到的方法:
$("#frameName").ready(function() {
// Write you frame on load javascript code here
} );
它對我不起作用。
這樣做:
$("#frameName").load( function() {
//code goes here
} );
即使事件沒有快速啟動 - 它等待圖像和css也加載。
我知道這是一個古老的話題。 但是為了幫助一些到達此頁面的人,這是我的解決方案:
$($("#frameName")[0].contentWindow.document).ready(function() {
// Write you frame onready code here
});
如果要為幀onload
事件,請按照下列步驟操作:
為每個<frame>
標記分配id
和name
。 確保id
和name
屬性值相同。
使用以下代碼來觸發幀的onload
事件:
$("frameName").ready(function() { // Write your frame onload code here }
我認為這是我在iframe中使用DOMContentLoaded時遇到的類似問題。
我寫了一篇關於它的博客文章 。
以下內容對我也有用:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script>
$(window.parent.frames[0].document).ready(function() {
// Do stuff
});
</script>
[0]表示它是文檔中的第一幀,[1]是第二幀,依此類推。 如果您無法控制標記,並且仍在使用文檔准備就緒,這一點尤其好。
我在這篇文章上工作了很長時間......這是我的解決方案。
的test.html
<!DOCTYPE HTML>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
document.write('<frameset><frame name="frame_content" id="frame_content"></frame></frameset>');
$('#frame_content').attr('src', 'test2.html');
$('#frame_content').load(function()
{
if('${"#header"}' != '') {
$("#header", frame_content.document).remove();
}
});
if($('#frame_content').complete) $('#frame_content').trigger("load");
</script>
</head>
</html>
test2.html
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="header">You will never see me, cause I have been removed!</div>
</body>
</html>
您是否嘗試將jQuery代碼放在Info.aspx頁面中?
我不知道它是否是最好的解決方案,但是當我刪除$(document).ready()
並保留它的正文時,一切都很完美。
不知道你想要做什么,但我有一個更老的經典asp應用程序,它運行框架,我剛剛添加了jQuery功能,它工作得很好。 $(document).ready()在一個框架內工作正常,但如果你想在另一個框架中引用DOM,你必須使用Frame的onload事件讓你知道何時加載了框架的DOM。 不可否認,我使用的是iFrame,但概念應該是相同的。
無需修改標記。 只需修復選擇器。 它應該是:
$("frame[name='main']").ready(function(){..});
不
$("#frameName").ready(function(){..});
注意:似乎jQuery ready事件多次觸發。 確保你的邏輯沒問題。
這個答案可能會遲到,但這個回復可能會對像我這樣的人有所幫助......
這可以通過原生Javascript代碼完成 -
ifrm2 = var ifrm2 = document.getElementById('frm2');
if (ifrm2.contentDocument.readyState == 'complete') {
//here goes the code after frame fully loaded
}
//id = frm2 is the id of iframe in my page
沒有理由不調用$(document).ready()
。 確保您的頁面包含jquery.js
包含。 嘗試使用空的HTML頁面進行簡單的測試,只需一個警報即可查看是否存在其他問題。
如果你試圖在包含框架定義的HTML頁面中使用它,請記住那里沒有文檔,你將不得不使用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.