简体   繁体   English

页面加载后如何获取动态生成的文本? (jQuery的)

[英]How to get dynamically generated text after page loads? (jQuery)

so my jQuery loads a page that contains some Mako code which loads in content that Python spits out. 因此,我的jQuery加载了一个页面,其中包含一些Mako代码,该代码加载了Python吐出的内容。

在此处输入图片说明在此处输入图片说明

My problem is that I need to capture the text that gets generated by the Mako code into a jQuery variable, however nothing gets captured in the jQuery until the page is fully loaded. 我的问题是我需要将由Mako代码生成的文本捕获到jQuery变量中,但是在页面完全加载之前,jQuery中不会捕获任何内容。

How would you code in jQuery to do something or wait until the page is fully loaded? 您将如何在jQuery中编写代码以执行某些操作或等待页面完全加载? I tried using the $('#inbox_conversation').html() inside of $(document).ready(function () { however it just returns undefined . 我尝试在$(document).ready(function () {内使用$('#inbox_conversation').html() $(document).ready(function () {但是它只是返回undefined


Mako template code Mako模板代码

<p id="inbox_conversation">
    %if inbox_details.body:
        ${inbox_details.body}
    %endif
</p>

jQuery: in Main Class jQuery:在主类中

var navigateToInboxDetails = function(messageID) {
    WHOAT.networking.redirectToURL("#!"+inboxDetailsURL(messageID));
};

var combined_message = $('#inbox_conversation').html();
alert('combined_message = '+combined_message);
// ^ Returns nothing because it runs before HTML is loaded

jQuery: in Networking Class jQuery:在网络类中

//redirect to another page... as the name suggests
var redirectToURL = function (url) {
    window.location = url;
};

The Ajax flow: Ajax流程:

The code that is in the jQuery: in Main Class and jQuery: in Networking Class sections gets activated after the this ajax 在此ajax之后,将激活jQuery:Main ClassjQuery:Networking Class部分中的代码

In my wireInbox function: 在我的wireInbox函数中:

var loadInbox = function() {
    loadResource(inboxURL(), null, function() {
        wireInbox();
        wireTableHighlighting();
    });
};

The loadResource function loadResource函数

var loadResource = function(url, params, callback) {
    WHOAT.networking.getToServerWithAjax(url, params, function (response) {
        //var $content = $($.parseHTML(response.trim()));
        var $content = $($.parseHTML($.trim(response)));
        var $container = $('#dashboard-display');
        var $content_to_hide = $container.children();

        $.when($content_to_hide.fadeOut('fast')).then(function () {
            $content.hide();
            $container.append($content);

            $content.fadeIn('fast', function(){
                $content_to_hide.remove();

                if(callback) {
                    callback();
                }
            });
        });
    });

Note: If I place $('#inbox_conversation').html(); 注意:如果我放置$('#inbox_conversation')。html(); on a button action, then it will alert me with the content I'm needing to get. 按下按钮操作,它就会提醒我需要获取的内容。 So I know the issue is that my it's a page hasn't finish rendering issue. 所以我知道问题是我的页面还没有完成渲染问题。

What I'm attempting to do is get the text from inside the P tag, then split it and put part of it into another div. 我想做的是从P标签中获取文本,然后将其拆分并将其一部分放入另一个div中。 How would you accomplish this? 您将如何实现?

Make sure jQuery is loaded after the relevant HTML you want to update: 确保要更新的相关HTML 之后加载了jQuery:

<p id="inbox_conversation">
    %if inbox_details.body:
        ${inbox_details.body}
    %endif
</p>
...
<script type='text/javascript' src='PATH_TO_JQUERY'></script>

You just can't get data from a page until it is loaded. 您只是无法从页面获取数据,直到页面被加载。

$(document).ready(function () {
    var combined_message = $('#inbox_conversation').html();
    alert('combined_message = '+combined_message);
});

Your attempt to put $('#inbox_conversation').html() inside of $(document).ready(function () { }); 您试图将$('#inbox_conversation').html()放入$(document).ready(function () { }); failed, because it was called asynchronously, ie sometime in the future. 失败,因为它是异步调用的,即在将来的某个时间。 So it can't return anything useful. 因此它无法返回任何有用的信息。 You need to wait until your page is loaded and then manipulate it. 您需要等到页面加载后再进行操作。 What you were trying to do is basically to travel back in time. 您想做的基本上是回溯过去。 You first tried to execute alert(value) and only after that you actually loaded value . 您首先尝试执行alert(value)然后才实际加载value

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

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