简体   繁体   English

使用jQuery / Javascript从iframe源获取元标签和img标签的内容

[英]get meta tags and img tags contents from an iframe source using jQuery/Javascript

I have an HTML file with the following content 我有一个包含以下内容的HTML文件

<html>
<head>
<meta charset="utf-8">
<title>builder 1.0</title>
<body>
<iframe width="100%" height="100%" src="a.html" name="cxsideframe" scrolling:'no';="" id="frame1" style="overflow:hidden;">
</iframe>
</body></html>

The content of a.html is a.html的内容是

<html>
<head>
<meta charset="utf-8">
<title>builder v1.0</title>

<meta content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" name="viewport">

<meta content="desc goes here" name="description">
<meta content="auth name" name="author">
<body>
<img src="http://example.com/images/01._V397411194_.png" style="display:none" alt=""/>
<img src="http://example.com/images/V386942464_.gif" style="display:none" alt="" />
</body></html>

I need to extract the contents of meta tags and image lists of a.html from the parent page using jQuery or javascript. 我需要使用jQuery或javascript从父页面中提取a.html的元标记和图像列表的内容。

I tried 我试过了

alert($('#frame1').contents().find($('#meta[name=description]').attr("content")).html());


and 
$('meta[name=author]').attr("content");      

But did not got the solution.Any idea on how to do this? 但是没有解决方案,关于如何做到这一点的任何想法?

To get the value of attribute content Use 获取属性content的值使用

alert($('#frame1').contents().find('meta[name=description]').attr("content"));

and to get all the images as array 并获取所有图像作为数组

$('#frame1').contents().find('img')

EDIT : Make sure that you are calling these methods after the iframe content has been loaded. 编辑:确保在加载iframe内容后正在调用这些方法。 So use 所以用

$("#frame1").ready(function(){
alert($('#frame1').contents().find('meta[name=description]').attr("content"));
alert($('#frame1').contents().find('img'));
})

for example place a button and on button get content of iframe 例如放置一个按钮,然后打开按钮获取iframe的内容

function getContentFromIframe(){
    var myIFrame=document.getElementById('iframe');
    var content=myIFrame.contentWindow.document.body.innerHTML;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM