简体   繁体   English

如何从.load()的页面访问CSS属性?

[英]How can i access a css attribute from a .load()'ed page?

I want to load an entire website in to my website. 我想将整个网站加载到我的网站中。 I have been able to do this with $(#preview).load("index.php") into my <div id="preview"></div> . 我已经可以通过$(#preview).load("index.php")到我的<div id="preview"></div>来做到这一点。 What i have troubles accessing here is the background image which is a property of body. 我在这里遇到的麻烦是背景图像,它是身体的属性。 I tried making a div tag with a background-image attribute but when i removed the image from my body tag it didnt behave like i wanted to (was not filling the entire space). 我尝试制作一个带有background-image属性的div标签,但是当我从body标签中删除该图像时,它的行为不像我想要的那样(未填充整个空间)。

My question is this. 我的问题是这个。 How can i access something from index.php that can let me either preview the site correctly or copy the attribute from somewhere into the preview background-image attribute? 我如何从index.php访问某些内容,可以使我正确预览网站或将属性从某处复制到预览背景图像属性中?

my code now looks like this, after some extensive try-and-error (more like error-and-error and its getting more and more messy) 经过大量尝试和错误之后,我的代码现在看起来像这样(更像是错误和错误,并且变得越来越杂乱)

    $(document).ready(
        function() {    
            $("#check").click(
                function() {
                    var bgd;
                    $("#preview").load("index.php", 
                        function () {
                            bgd = $("#bg").css("background-image");
                        }
                    );
                    $("#preview").style.backgroundImage(bgd);
                }
            );
        }
    );

Where bg is the id of the div which works as a "substitute" body tag in index.php (aka with the same attributes as body) 其中bg是div的ID,它用作index.php中的“替代”主体标签(也具有与主体相同的属性)

Im either far from it, or ridiculously close. 我要么离它很远,要么离谱。 Thanks for every piece of advice i can get. 感谢您可以获得的每条建议。

   $(document).ready(
        function() {    
            $("#check").click(
                function() {
                    $.ajax({
                             url: 'index.php',
                             success: function(data){
                                 data = $('data').html();
                                 $('#preview').html(data);
                                 bg = $('data').find('#bg').css('background-image');
                                 $('#preview').css('background-image',bg);
                             }
                        });

                }
            );
        }
    );

Two problems here. 这里有两个问题。 One with how you're accessing style properties and the other has to do with the asynchronous behavior of load() . 一个与您访问样式属性的方式有关,另一个与load()的异步行为有关。

Style problem 风格问题

You'll need to use $("#preview").css('background-image',bgd) . 您需要使用$("#preview").css('background-image',bgd) The first method you were using could still be kept around by accessing the non-jQuery wrapped element, like so: 通过使用非jQuery包装的元素,仍然可以使用您使用的第一种方法,如下所示:

$("#preview")[0].style.backgroundImage(bgd);

Async problem 异步问题

The second issue is that .load() is an asynchronous call that returns immediately . 第二个问题是.load()是立即返回的异步调用。 The next line of code gets executed and bdg is (most likely) still undefined. 下一行代码将被执行,并且bdg (很有可能)仍未定义。 Then when load() completes, the success handler gets executed and bdg gets set to the background of the loaded page, but it's too late! 然后,当load()完成时,将执行成功处理程序,并将bdg设置为已加载页面的背景,但是为时已晚!

Moving the $("#preview").css('background-image',bgd) into the success handler will rectify that problem: $("#preview").css('background-image',bgd)移至成功处理程序将纠正该问题:

   $(document).ready(
        function() {    
            $("#check").click(
                function() {
                    var bgd;
                    $("#preview").load("index.php", 
                        function () {
                            bgd = $("#bg").css("background-image");
                            $("#preview").css('background-image',bgd);
                        }
                    );
                }
            );
        }
    );

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

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