同域jQuery(跨)iframe操作DOM

來源:互聯網
上載者:User

同域jQuery(跨)iframe操作DOM

 本篇文章主要是對同域jQuery(跨)iframe操作DOM進行了詳細的介紹,需要的朋友可以過來參考下,希望對大家有所協助

frame目前還是比較流行的,許多地方都通過它來實現特殊的情況。比如說傳統的上傳、select在ie6下、代理、跨域等等。今天呢,簡要的記述一下跨iframe的相關操作,主要是用jQuery操作DOM結構方面。

 

代碼如下:

<iframe src="a.php" id="aa"></iframe> 

<iframe src="b.php" id="bb"></iframe> 

<input type="button" id="read-aa" value="讀取iframe #aa"> 

<input type="button" id="write-aa" value="寫入iframe #aa">

 

代碼如下:

$('#read-aa').click(function() 

    var v=$('#aa').contents().find('body').html(); 

    alert(v); 

}); 

$('#write-aa').click(function() 

 

    $('#aa').contents().find('div').append('<hr>這是index.php操作aa.php寫入的內容'); 

});

 

主要方法是 contents(),讀取iframe。

 

2、iframe跨父架構操作iframe

 

代碼如下:

<!DOCTYPE html> 

<meta charset="utf-8"> 

<title>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</title> 

<script src="public/jQuery.js"></script> 

<script> 

$(function() 

    $('#read-parent-aa').click(function() 

    { 

        var v=$('body',parent.document).find('#aa').contents().find('body').html(); 

        alert(v); 

    }); 

    $('#write-parent-aa').click(function() 

    { 

        $('body',parent.document).find('#aa').contents().find('div').append('<hr>這是bb.php操作aa.php寫入的內容'); 

    }); 

}); 

</script> 

<div> 

這是iframe #bb裡的內容 

</div> 

<input type="button" id="read-parent-aa" value="跨父讀取iframe #aa"> 

<input type="button" id="write-parent-aa" value="跨父寫入iframe #aa">

 

HTML:代碼

代碼如下:

<!DOCTYPE html>

<meta charset="utf-8">

<title>jQuery操作iframe</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<iframe src="a.php" id="aa"></iframe>

<iframe src="b.php" id="bb"></iframe>

<input type="button" id="read-aa" value="讀取iframe #aa">

<input type="button" id="write-aa" value="寫入iframe #aa">

<script> 

$(function()

{

 $('#read-aa').click(function()

 {

  var v=$('#aa').contents().find('body').html();

  alert(v);

 });

 $('#write-aa').click(function()

 {

 

  $('#aa').contents().find('div').append('<hr>這是index.php操作aa.php寫入的內容');

 });

});

</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.