如何将iframe内容复制到div? [英] How to copy iframe content to div?

查看:137
本文介绍了如何将iframe内容复制到div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的代码片段。我希望iframe中的输出文本显示在#source div中。我很努力,并对任何想法表示感谢。如何将iframe中的输出文本复制到div?
(如果div #sourcediv包含Text,则该脚本在div #show中写入Can see text!,否则Can not see text!。)

 < html>< body>< div id =source>< / div>< div id =节目>< / DIV> <脚本>如果(document.getElementById('source')。innerHTML.indexOf(Text)!= -1){document.getElementById('show')。innerHTML =Can see text!;} else {document.getElementById 'show')。innerHTML =Can not see text!;}< / script>< iframe id =iframesrc =http://majaulrika.esy.es/text.txt>< >< / html>  

解决方案

进一步您的评论

从文件获取内容的更好的解决方案是使用ajax。



这是一种简单的使用ajax和jQuery。


$ b

  $。ajax({
url:'http: //output.jsbin.com/sebusu',
success:function(data){
$('#source')。html(data);
$('#show') .text(function(){
if(data.indexOf('Text')!= -1){
返回n'可以看到文本!';
}
else {
returnCan not see text!;
}
});
},
错误:function(){
console.log(error);
}
});



< div id = 源 >< / DIV>
< div id =show>< / div>
< script src =https://code.jquery.com/jquery-3.1.0.js>< / script>

现场演示



再次,您只能为您的网站调用ajax(有一些异常


See code snippet below. I want the output text in the iframe to show in the #source div. I’m struggeling with this, and am grateful for any ideas. How can I copy the output text in the iframe to a div? (The script writes "Can see text!" in div #show if div #sourcediv contains "Text", else "Cannot see text!".)

<html>
<body>

<div id="source"></div>
<div id="show"></div> 

<script> 
if (document.getElementById('source').innerHTML.indexOf("Text") != -1)
{document.getElementById('show').innerHTML="Can see text!";}
else{document.getElementById('show').innerHTML="Cannot see text!";}
</script>

<iframe id="iframe" src="http://majaulrika.esy.es/text.txt">

</body>
</html>

解决方案

Further your comment:

A better solution to get a content from file is to use ajax.

Here is a simple approach of using ajax with jQuery.

$.ajax({
  url: 'http://output.jsbin.com/sebusu',
  success: function(data) {
    $('#source').html(data);
    $('#show').text(function(){
      if (data.indexOf('Text') != -1) {
        return 'Can see text!';
      }
      else {
        return "Can't see text!";
      }
    });
  },
  error: function(){
    console.log(error);
  }
});

<div id="source"></div>
<div id="show"></div>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

Live Demo

Again, you can call ajax only for your website (with some exceptions)

这篇关于如何将iframe内容复制到div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆