javascript - 使用 Ajax 或 Jsonp 导入 URL,无需使用 iframe

标签 javascript php jquery ajax iframe

我有这个简单的索引:

<head>
<script src="js/jquery-1.7.1.js" type="text/javascript"></script>
</head>
<body>
<div style="bottom: 0px;position: absolute;">
    <? include("inc/creditos.php")?>
</div>
</body>
</html>

creditos.php:

<script>
// JavaScript Document
$(document).ready(function(){                   
    $('#show_credits').click(function(){
        //$('iframe#cred').css("display","none");
        var pos=$(this).position();
        if (pos.left>200) pos.left=pos.left-250;                                  
            $('#cred').css("top",pos.top-240);                            
            $('#cred').css("left",pos.left);
            $('#cred').fadeIn("slow");
            //$("iframe#cred").contents().find("#creditos").css('display','none');
            $('#close').css("background-color","red");  
            $('#close').css("top",pos.top-223);                           
            $('#close').css("left",pos.left+293);
            $('#close').css("position","absolute");
            $('#close').fadeIn("slow");
            $('#close').css("z-index","1");
            $('#close').css("width","20px");
            $('#close').css("height","20px");           
        });

    $('#close').click(function(){
        $('#cred').fadeOut("slow");
        $('#close').fadeOut("slow");

});
    }); 
</script>
<iframe style="position:absolute;width:313px;height:234px;display:none;border:0;"id="cred" name="test_creditos" src="http://img1.wikia.nocookie.net/__cb20100909154419/mario/es/images/5/54/Flor_de_Fuego.jpg" scrolling="no"></iframe>
<a class="credits" href="javascript:void(0)" id="show_credits">Cr&eacute;ditos</a>
<a href="javascript:void(0)" class="close" id="close" style="display:none;"></a>

我可以在没有 iframe 的情况下使用 Ajax 或 Jsonp 做同样的事情吗(当我单击“Creditos”时调用并导入图像)?

我需要删除 iframe 并得到相同的结果:/

最佳答案

您可以使用 jQuery.get() 检索 URL 上的文件,然后对内容执行任何您想要的操作:https://api.jquery.com/jQuery.get/

关于javascript - 使用 Ajax 或 Jsonp 导入 URL,无需使用 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22766052/

相关文章:

javascript - 如何在 NPM 包中使用 webpack 动态导入?

JavaScript:混合数组/对象的奇怪(边缘?)情况

javascript - 延迟加载下一页 fullpage.js 不起作用

javascript - 循环遍历 json 对象数组

javascript - 将 angular.forEach 与 JSON 结合使用

javascript - 将音频isPlaying状态设置为当前音频唯一

php - 将 php 错误传递给我的自定义错误处理程序中的默认 php 错误处理程序

javascript - 如何将一种格式的json转换为另一种格式

php - Codeigniter 3 上传到 Linux 主机

php - 搜索多个表php activerecord