简体   繁体   English

jQuery-如何从我发现的Ajax注入脚本制作iframe?

[英]jquery - how to make an iframe from a script i found into an ajax injection?

I'm wondering how i would change this appended iframe: 我想知道如何更改此附加的iframe:

$("#GB_window").append("<iframe id='GB_frame' src='" + url + "'></iframe>");

into a div. 进入div Is there a way to change this so that i'm not appending an iframe, but instead using ajax? 有没有一种方法可以更改此设置,以便我不附加iframe,而是使用ajax? I need to be able to use the 我需要能够使用

src='" + url + "'

part of it. 一部分。 i'm still a novice. 我仍然是新手。

If you'd like to see the script i'm editing, it's the GreyBox Redux lightbox script by John Resig from awhile ago. 如果您想查看我正在编辑的脚本,那是John Resig不久前的GreyBox Redux灯箱脚本。 I've stripped a decent amount of jquery out of it so far, because all i need it for showing images, not websites. 到目前为止,我已经删除了相当数量的jQuery,因为我只需要它来显示图像,而不是网站。 it's only a couple of kb too. 也只有几kb。

thanks. 谢谢。

here's the jquery i have left after what i've stripped: 这是我剥离后剩下的jQuery:

    var GB_DONE = false;

function GB_show(caption, url, height, width){
    GB_HEIGHT = height || 400;
    GB_WIDTH = width || 400;
    if (!GB_DONE) {
        $(document.body).append("<div id='GB_overlay'></div><div id='GB_window'>");
        $("#GB_overlay").click(GB_hide);
        $(window).resize(GB_position);
        GB_DONE = true;
    }

    $("#GB_frame").remove();
    $("#GB_window").append("<iframe id='GB_frame' src='" + url + "'></iframe>");

    $("#GB_overlay").fadeIn(350, function(){
        $("#GB_window").fadeIn(350);
    });
    GB_position();
}

function GB_hide(){
$("#GB_window,#GB_overlay").fadeOut(350, function(){
    $("#GB_window,#GB_overlay").hide()
});
}

function GB_position(){
var de = document.documentElement;
var w = self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
$("#GB_window").css({
    width: GB_WIDTH + "px",
    height: GB_HEIGHT + "px",
    left: ((w - GB_WIDTH) / 2) + "px"
});
$("#GB_frame").css("height", GB_HEIGHT - 32 + "px");

and this is from the of the html file: 这是来自html文件的:

    <script type="text/javascript">
  var GB_ANIMATION = true;
  $(document).ready(function(){
    $("a.greybox").click(function(){
      GB_show(this.title || $(this).text() || this.href,this.href,470,600);
      return false;
    });
  });
</script>

sorry if that's a lot :/ 抱歉,如果很多的话:/

Instead of append("<iframe>...."); 而不是append("<iframe>...."); line use this one 行使用这个

$("<div id='GB_frame'></div>").appendTo("#GB_window").load(url);

It will create new div with id GB_iframe , append it to GB_window and then load content from provided url right into it. 它将创建ID为GB_iframediv ,将其附加到GB_window ,然后将提供的URL中的内容直接加载到其中。

If I understand you correctly, you want to load the contents of an html file and place it on a section of the page using ajax, not an iframe? 如果我理解正确,那么您想加载html文件的内容,然后使用ajax(而不是iframe)将其放置在页面的某个部分上吗?

That's fairly simple using the jQuery load method. 使用jQuery load方法非常简单。

$("#GB_window").load("url to load from");

This will place the contents of the html file you point to as the contents of the elements with the id 'GB_window'. 这会将您指向的html文件的内容作为ID为'GB_window'的元素的内容放置。

edit 编辑

So, you're trying to show an image file? 因此,您要显示图像文件吗?

You need to use an <img> tag. 您需要使用<img>标签。 Try this: 尝试这个:

$("img").attr("src", "url to image").appendTo("#GB_window");

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

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