2015-12-23 56 views
2

這是這個問題的延續加載元素: jquery: how to detect element that is being loaded every few secondsjQuery的:不是說重點每隔幾秒鐘

原來我的代碼從上面的鏈接^工作正常。我不知道爲什麼它一開始不工作。

的現在,而不是提醒上點擊..

我試圖滾動到從屬性HREF的元素。

<a href="#scrolltothiselement" class="gotobtn">click</a> 
... 
<div id="scrolltothiselement"></div> 
... 

我這是怎麼做的吧:

$(document).on('click', '.gotobtn', function() { 

    var gotothisid = $(this).attr('href'); 
    $(document).find(gotothisid).focus(); 

    return false; 
}); 

我也試過:

$(document).unbind().on('click', '.gotobtn', function(event) { 
    event.preventDefault(); 
    var gotothisid = $(this).attr('href'); 
    $(document).find(gotothisid).focus(); 
}); 

它不工作:「(

注:

<a href="#scrolltothiselement" class="gotobtn">click</a> 
... 
<div id="scrolltothiselement"></div> 
... 

裝入的index.html像

setInterval(
function() 
{ 
$('.loadHere').unload().load('filetoload.php').fadeIn('slow'); 

}, 10500); 

我已經在上面的鏈接解釋它:P

更多細節: 這不是一切,但我覺得有一個很好的機會問題是我在下面顯示的代碼中的某處。我也改變了變量名和類名。對不起,我無法顯示所有內容,因爲它是保密的。

的index.php

<?php include_once 'header.php'; ?> 
<input type="hidden" class="currentlyvisibletab" value="" /> 


<div class="AllDateTimeTabs"> 


    <div id="today" class="datetimetab today-cont"> 
     <?php include_once 'today.php'; ?> 
    </div> 

    <div id="tomorrow" class="datetimetab tomorrow-cont"> 
     <?php include_once 'tomorrow.php'; ?> 
    </div> 

    <div id="yesterday" class="datetimetab yesterday-cont"> 
     <?php include_once 'yesterday.php'; ?> 
    </div> 


</div> 
<div id="testscrollhere">Scroll here. animate code works here.</div> 

<?php include_once 'footer.php'; ?> 

today.php,tomorrow.php,yesterday.php具有類似的結構只是不同的查詢。

<?php 

include_once 'connect.php'; 

$thisfiledate = date('Y-m-d'); 



$result = $conn->prepare("SELECT * FROM tbname WHERE field= :thisfiledate AND anotherfield= 'value';"); 
$result -> bindParam(':thisfiledate', $thisfiledate); 
$result->execute(); 


$displaydate = 'Today '.$thisfiledate; 
include 'maincontent.php'; 


?> 

maincontent.php - 我會刪除某些部分,因爲他們是保密的。但你明白了。 maincontent.php有while循環,它顯示從表中選擇的東西。從表中的每一行都有自己的

<div id="'.$row['rownumber'].'">details goes here</div>

有一個在頂部的贏家按鈕,如果你點擊它,它會滾動到這是勝利者的行。只有一個贏家。獲勝者按鈕是

`<a href="#123" class="gotobtn">123</a>` 

如上所述。

<?php 

... 

while ($row = $result->fetch(PDO::FETCH_ASSOC)) 
{ 


    ... 



    $displayall .= '<div class="col-md-1 col-sm-2 col-xs-4 c-cont-col">'; 
     $displayall .= '<div class="c-cont '.$cyellow .'" id="'.$row['rownumber'].'">'; 
      $displayall .= '<h4 class="winnerlabel '.$wiinerlavelinvisibility .'">WINNER</h4>'; 
      $displayall .= '<h4 class="cn-label '.$labelcolor.'">'. $row['rownumber']. '</h4>'; 
      $displayall .='<div class="ci-cont">'; 
      //$displayall .= '<p><b>Date:</b> '.$row['cut_off_date_tmstrans'].'</p>'; 
      $displayall .= '<p><b>label:</b><br>'.number_format($row['x'],2).'</p>'; 
      $displayall .= '<p><b>label2: </b><br>'.number_format($row['y'],2).'</p>'; 
      $displayall .= '<p><b>label3: </b><br>'.number_format($row['z'],2).'</p>'; 
      $displayall .= '</div>'; 
     $displayall .= '</div>'; 
    $displayall .= '</div>'; 


} 

if($haswinner == 0) 
{ 
    $winnerboxinvisibility = 'invisibility'; 
} 
else 
{ 
    $winnerboxinvisibility = ''; 
} 

echo '<div class="row">'; 
echo '<div class="col-md-6 col-sm-4 col-xs-12 date-cont-col">  <div class="pull-left date-cont">'.$displaydate.' <div class="zerocountercolordot"></div> '. $zerocounter.' <div class="lessorequaltotencolordot"></div> '.$lessorequaltotencounter.' <div class="lessorequaltotwohundredcolordot"></div> '.$lessorequaltotwohundredcounter.' <div class="greterthantwohundercolordot"></div> '.$greterthantwohundercounter.'</div></div>'; 
echo '<a href="#'.$winningc.'" class="gotobtn"><div class="col-md-4 col-sm-5 col-xs-12 winning-c-col"> <div class="pull-right winning-c '.$winnerboxinvisibility.'"><p><b>Winner: </b>'.$winningc.'</div></div></a>'; 
echo '<div class="col-md-2 col-sm-3 col-xs-12 total-cont-col"> <div class="pull-right total-cont"><p><b>Label: </b>'.number_format($variablename,2).'</p></div></div>'; 
echo '</div>'; 

echo '<div class="row">'; 
echo $displayall; 
echo '</div>'; 


?> 

custom.js

var currentlyvisibletab; 

$('.nav.navbar-nav a').on('click',function(event) 
{ 
    event.preventDefault(); 



    loadthisdatetimetab = $(this).attr('href'); 
    $('.datetimetab').hide(); 
    $(loadthisdatetimetab).show(); 

    $('.currentlyvisibletab').val(loadthisdatetimetab); 
    currentlyvisibletab = loadthisdatetimetab; 


}); 

setInterval(
function() 
{ 
    $.ajax(
      { 
       type: "POST", 
       url: "timecheck.php", 
       datatype: "json", 
       success: function(data) 
       { 

        if(data != 'no')//if not scheduled time to change tabs 
        { 
         if($('.currentlyvisibletab').val() != data) 
         {//data is either #today, #tomorrow , #yesterday 
          $('.currentlyvisibletab').val(data); 
          currentlyvisibletab = data; 


          $(currentlyvisibletab).siblings().hide(); 
          $(data).show(); 


         } 
        } 
       } 
      }); 
}, 3500); 

function() 
{ 
$('#today').unload().load('today.php').fadeIn('slow'); 




$('#tomorrow').unload().load('tomorrow.php').fadeIn('slow'); 



$('#yesterday').unload().load('yesterday.php').fadeIn('slow'); 

$(currentlyvisibletab).siblings().hide(); 
$(currentlyvisibletab).show(); 
}, 10599); 



function onloadct()/*for <body onload="onloadct()">*/ 
{ 

    if(window.location.hash) 
    { 
     // Fragment exists 
     var hashvalue = window.location.hash; 


     $('.datetimetab').hide(); 
     $(hashvalue).show(); 

     currentlyvisibletab = hashvalue; 

    } else 
    { 
     // Fragment doesn't exist 

     $.ajax(
      { 
       type: "POST", 
       url: "onloadchecktime.php", 

       datatype: "json", 
       success: function(data) 
       { 


         if($('.currentlyvisibletab').val() != data) 
         { 
          $('.currentlyvisibletab').val(data); 
          currentlyvisibletab = data; 


          $(currentlyvisibletab).siblings().hide(); 
          $(data).show(); 


         } 

       } 
      }); 
    } 

} 


/*as suggested in the answer and comments but still doesn't work. removed unbind because it stopped twitter bootstrap navbar from working when collapsed */ 



    $(document).ready(function() { 
     $(document).on('click', '.gotobtn', function(event) 
    { 


    // prevent default behavior (getting the # in the URL) 
    event.preventDefault(); 
    // get the id of the element that you want to scroll to 
    var gotothisid = $(this).attr('href'); 
    // scroll the html/body as many pixels as the target element's position 
    $("body").animate({ scrollTop: $(gotothisid).offset().top }); 
    }); 
}); 
+1

問:你需要的( 「點擊」 ......在所有??? –

+0

爲什麼不讓'HREF = 「#scrolltothiselement」 '自然做它的事? – andi

+0

你想要做一個平滑滾動,而不是跳躍元素,你會通過哈希HREF得到正常? –

回答

-2

你應該閱讀有關錨看到你正在試圖做的是瀏覽器的默認行爲。

+0

我知道..我試圖阻止#stuff的網址,使我的代碼更簡單。我已經有代碼,使得我的網站行爲不同,如果它檢測到一些#stuff上的url。 –

-3

嘗試處理分配到父element.Notice的a在在你的代碼所缺少的選擇。

$(document).on('click','a.gotobtn',function() 
    { 

      //your code 


    }); 

on方法可以綁定到父元素,然後點擊將動態附加到原始DOM設置後加載的元素的任何子元素。只要你附加到一個新的元素被稱爲「gotobtn」它應該工作。

您可以在深度答案更here

+1

OP已經有代表團 –

+0

這與OP說她嘗試過的沒有什麼不同 – andi

+0

他沒有委託,他直接將處理程序分配給'gotobtn',而不是它的父節點元素,看看我的cod關閉然後他的。 – Korgrue

0

一旦鏈接被點擊(防止這將是默認行爲後,您可以通過使用animate()方法,並滾動HTML /身體的元素實現這一目標把#放在URL中)。

要計算要滾動的像素數量,可以使用返回元素座標的方法offset()(您將集中在.offset().top進行垂直滾動)。然後使用與Nick's solution中解釋的方法類似的方法滾動htmlbody並使用scrollTop

一個簡單的演示:

$(document).ready(function() { 
 
    $(document).unbind().on('click', '.gotobtn', function(event) { 
 
    // prevent default behavior (getting the # in the URL) 
 
    event.preventDefault(); 
 
    // get the id of the element that you want to scroll to 
 
    var gotothisid = $(this).attr('href'); 
 
    // scroll the html/body as many pixels as the target element's position 
 
    $("html, body").animate({ scrollTop: $(gotothisid).offset().top }); 
 
    }); 
 
});
p { margin-bottom: 12px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p><a href="#scrolltothiselement" class="gotobtn">click</a></p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<div id="scrolltothiselement">Yes! Scroll here.</div> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p> 
 
<p>Nope.</p>

+0

謝謝。我會試試這個。 –

+0

它沒有與我想要滾動的元素一起工作。它工作時,雖然我試過

測試。一個是index.php的一部分。我想要滾動到的那個像$('。loadhere')那樣加載。load('content.php'); content.php包含錨和
Yes! Scroll here.

+0

我也刪除了unbind(),因爲它使我的twitter引導程序navbar在摺疊時停止工作。但它仍然滾動到

相關問題