简体   繁体   English

如何使用jquery加载检查加载的div标签的内容?

[英]How to check for contents of a loaded div tag using jquery load?

I'm working with jqueries address change event and am hitting a roadblock when a user copies and pastes a URL in the browser. 我正在处理jqueries地址更改事件,当用户在浏览器中复制并粘贴URL时遇到障碍。 I need to fist load a portion of the page that contains a form. 我需要首先加载包含表单的页面的一部分。 I could do this after every pagination call but it seems really ineffecient. 我可以在每次分页调用之后执行此操作,但是这似乎效率很低。

Here is my current code block: 这是我当前的代码块:

$.address.change(function(e) {

    var urlAux = e.value.split('=');
    var page   = urlAux[0];
    var start  = urlAux[1];

    if (page == "/visits") {

        $.address.title("Profile Views");

        if (start) {

            $('#start').val(start);

            // ***** If a user has copied and pasted this URL with a start value then I first need to load visits.php in the main div tag.  Is it possible to see if this is loaded or not?

            $.post("visits_results.php", $("#profile_form_id").serialize(),
                function(data) {
                    $('#search_results').html(data);
                    location.href = "#visits=" + start; 
                });

        }
        else {
            var args = localStorage.getItem("visits");

            $('#main').load("visits.php?" + args, function () { }); 
        }
    }

My attempted work around was this: 我尝试解决的问题是这样的:

var args = localStorage.getItem("visits");

$('#main').load("visits.php?" + args, function () { 

    $('#start').val(start);

    $.post("visits_results.php", $("#profile_form_id").serialize(),
        function(data) {
            $('#search_results').html(data);
            location.href = "#visits=" + start; 
         });
    }); 

There must be a better way...this is realoading the same portion of the page (visits.php) with every pagination event. 必须有更好的方法...这是在每次分页事件中重新加载页面的相同部分(visits.php)。 Is there a better way to load URLs and not have them trigger an address change? 有没有更好的方法来加载URL而不让它们触发地址更改?

Using paul's work around from his comments, but instead of Regex'ing html content in the visits.php form this solution will look for data() attached to #mainID. 使用paul从他的评论中解决的方法,而不是Regex在visits.php表单中显示html内容,此解决方案将查找附加到#mainID的data()。

Paul's work around notes: 保罗的工作围绕着笔记:

After a bit more hacking I came up with this solution that seems to do the trick. 经过更多的黑客攻击后,我想出了这个解决方案,似乎可以解决问题。 I'm not sure how good it is but it seems to do the trick. 我不确定它有多好,但似乎可以解决问题。 I now get the main div id and do a regex match on a unique string in the form. 现在,我得到主要的div ID并在表单中的唯一字符串上进行正则表达式匹配。 If I don't see it I load the form and then load the results. 如果看不到,请加载表格,然后加载结果。 Not sure if this is good practice or not but it seems to solve my issue. 不知道这是否是个好习惯,但似乎可以解决我的问题。

Methodology to use .data() instead of a regex search of visits.php's html: 使用.data()而不是visits.php的html的正则表达式搜索的方法:

    /*check if we're missing visits.php by looking for data() flag*/
             if( !($("#main").data()["hasVisitsPhp"]) ){
              var args = localStorage.getItem("visits");
              $('#main').load("visits.php?" + args, function () { 
                $('#start').val(start);
                $.post("visits_results.php", $("#profile_form_id").serialize(),
                    function(data) {
/* we've loaded visits.php, set the data flag on #main*/
                $('#main').data("hasVisitsPhp","loaded");
                        $('#search_results').html(data);
                        location.href = "#visits=" + start; 
                     });
                });
             }

try window.location.hash instead. 请尝试使用window.location.hash Changing the whole href can/will trigger a whole-page reload, while changing just the hash by itself should at most cause the page to scroll. 更改整个href可以/将触发整个页面的重新加载,而仅更改哈希本身最多应该导致页面滚动。

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

相关问题 提交按钮在div中不起作用(其内容在jQuery中使用.load()加载) - submit button won't work in div (contents of which are loaded using .load() in jQuery) 如何使用Jquery将php代码加载到html标签DIV中? - How to load php code into html tag DIV using Jquery? 使用ajax和jQuery时,如何识别完全加载或不加载的div中的ajax内容(图像和文本)? - How to identify ajax contents (images and text) in a div loaded completely or not when using ajax and jQuery? 如何在不删除divs现有内容的情况下使用jquery load()将内容加载到现有div中 - How to load contents to an existing div with jquery load() without removing the divs existing contents jQuery 功能无法使用.load() 刷新 div 标签 - jQuery functions not Working using .load() to refresh div tag 使用PHPMailer发送php文件的内容(使用Jquery加载方法动态加载) - Sending contents of a php file (dynamically loaded using Jquery load method) using PHPMailer JQuery 在页面加载时替换多个 div 内容 - JQuery to replace multiple div contents on page load 如何使用jQuery在div中加载php页面 - how to load php page in a div using jquery jQuery无法处理AJAX加载的内容来显示/隐藏div内容 - jQuery not working on AJAX-loaded content to show/hide div contents 如何使用jquery和PHP更改div内容 - How can I change div contents using jquery and PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM