Home > Web Front-end > JS Tutorial > jQuery cross-domain operation DOM method in iframe

jQuery cross-domain operation DOM method in iframe

php中世界最好的语言
Release: 2018-04-26 15:19:16
Original
6155 people have browsed it

This time I will bring you jQuery Cross-domain operation of the DOM method in the iframe, jQuery cross-domain operation of the DOM method in the iframe. What are the precautions? The following is a practical case. Get up and take a look.

frame is still quite popular and is used in many places to implement special situations. For example, traditional

upload, select under ie6, proxy, cross-domain, etc. Today, I will briefly describe the related operations across iframes, mainly using jQuery to operate the DOM structure.

<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">
Copy after login
$(&#39;#read-aa&#39;).click(function() 
{ 
    var v=$(&#39;#aa&#39;).contents().find(&#39;body&#39;).html(); 
    alert(v); 
}); 
$(&#39;#write-aa&#39;).click(function() 
{ 
    $(&#39;#aa&#39;).contents().find(&#39;p&#39;).append(&#39;<hr>这是index.php操作aa.php写入的内容&#39;); 
});
Copy after login

The main method is contents(), which reads iframe.

2. iframe cross-parent frame operation iframe

<!DOCTYPE html> 
<meta charset="utf-8"> 
<title>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</title> 
<script src="public/jQuery.js"></script> 
<script> 
$(function() 
{ 
    $(&#39;#read-parent-aa&#39;).click(function() 
    { 
        var v=$(&#39;body&#39;,parent.document).find(&#39;#aa&#39;).contents().find(&#39;body&#39;).html(); 
        alert(v); 
    }); 
    $(&#39;#write-parent-aa&#39;).click(function() 
    { 
        $(&#39;body&#39;,parent.document).find(&#39;#aa&#39;).contents().find(&#39;p&#39;).append(&#39;<hr>这是bb.php操作aa.php写入的内容&#39;); 
    }); 
}); 
</script> 
<p>
Copy after login

This is the content in iframe #bb


</p> 
<input type="button" id="read-parent-aa" value="跨父读取iframe #aa"> 
<input type="button" id="write-parent-aa" value="跨父写入iframe #aa">
Copy after login

HTML: code

<!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()
{
 $(&#39;#read-aa&#39;).click(function()
 {
  var v=$(&#39;#aa&#39;).contents().find(&#39;body&#39;).html();
  alert(v);
 });
 $(&#39;#write-aa&#39;).click(function()
 {
  $(&#39;#aa&#39;).contents().find(&#39;p&#39;).append(&#39;<hr>这是index.php操作aa.php写入的内容&#39;);
 });
});
</script>
Copy after login

believe After reading the case in this article, you have mastered the method. For more exciting information, please pay attention to php Chinese website

Other related articles!

Recommended reading:

JSONP solves ajax cross-domain problem (with code)

php jquery implementation method of passing json data

The above is the detailed content of jQuery cross-domain operation DOM method in iframe. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template