簡體   English   中英

如何使用jquery加載檢查加載的div標簽的內容?

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

我正在處理jqueries地址更改事件,當用戶在瀏覽器中復制並粘貼URL時遇到障礙。 我需要首先加載包含表單的頁面的一部分。 我可以在每次分頁調用之后執行此操作,但是這似乎效率很低。

這是我當前的代碼塊:

$.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 () { }); 
        }
    }

我嘗試解決的問題是這樣的:

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; 
         });
    }); 

必須有更好的方法...這是在每次分頁事件中重新加載頁面的相同部分(visits.php)。 有沒有更好的方法來加載URL而不讓它們觸發地址更改?

使用paul從他的評論中解決的方法,而不是Regex在visits.php表單中顯示html內容,此解決方案將查找附加到#mainID的data()。

保羅的工作圍繞着筆記:

經過更多的黑客攻擊后,我想出了這個解決方案,似乎可以解決問題。 我不確定它有多好,但似乎可以解決問題。 現在,我得到主要的div ID並在表單中的唯一字符串上進行正則表達式匹配。 如果看不到,請加載表格,然后加載結果。 不知道這是否是個好習慣,但似乎可以解決我的問題。

使用.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; 
                     });
                });
             }

請嘗試使用window.location.hash 更改整個href可以/將觸發整個頁面的重新加載,而僅更改哈希本身最多應該導致頁面滾動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM