2012-06-06 60 views
0

Iam獲取div標籤的offsetwidth。以下是代碼。獲取javascript錯誤對象需要

<body> 
    <div id="marqueeborder" onmouseover="pxptick=0" onmouseout="pxptick=scrollspeed"> 
<div id="marqueecontent"> 


<?php 

    // Original script by Walter Heitman Jr, first published on http://techblog.shanock.com 

    // List your stocks here, separated by commas, no spaces, in the order you want them displayed: 
    $stocks = "idt,iye,mill,pwer,spy,f,msft,x,sbux,sne,ge,dow,t"; 

    // Function to copy a stock quote CSV from Yahoo to the local cache. CSV contains symbol, price, and change 
    function upsfile($stock) { copy("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=sl1c1&e=.csv","stockcache/".$stock.".csv"); } 

    foreach (explode(",", $stocks) as $stock) { 

     // Where the stock quote info file should be... 
     $local_file = "stockcache/".$stock.".csv"; 

     // ...if it exists. If not, download it. 
     if (!file_exists($local_file)) { upsfile($stock); } 
     // Else,If it's out-of-date by 15 mins (900 seconds) or more, update it. 
     elseif (filemtime($local_file) <= (time() - 900)) { upsfile($stock); } 

     // Open the file, load our values into an array... 
     $local_file = fopen ("stockcache/".$stock.".csv","r"); 
     $stock_info = fgetcsv ($local_file, 1000, ","); 

     // ...format, and output them. I made the symbols into links to Yahoo's stock pages. 
     echo "<span class=\"stockbox\"><a href=\"http://finance.yahoo.com/q?s=".$stock_info[0]."\">".$stock_info[0]."</a> ".sprintf("%.2f",$stock_info[1])." <span style=\""; 
     // Green prices for up, red for down 
     if ($stock_info[2]>=0) { echo "color: #009900;\">&uarr;"; } 
     elseif ($stock_info[2]<0) { echo "color: #ff0000;\">&darr;"; } 
     echo sprintf("%.2f",abs($stock_info[2]))."</span></span>\n"; 
     // Done! 
     fclose($local_file); 
    } 
?> 
<span class="stockbox" style="font-size:0.6em">Quotes from <a href="http://finance.yahoo.com/">Yahoo Finance</a></span> 

</div> 
</div> 
</body> 

下面是將被稱爲頁面的onlaod的JavaScript函數。

<script type="text/javascript"> 

    // Original script by Walter Heitman Jr, first published on http://techblog.shanock.com 

    // Set an initial scroll speed. This equates to the number of pixels shifted per tick 
    var scrollspeed=2; 
    var pxptick=scrollspeed; 

    function startmarquee(){ 
     alert("hi"); 
     // Make a shortcut referencing our div with the content we want to scroll 
     var marqueediv=document.getElementById("marqueecontent"); 
     alert("marqueediv"+marqueediv); 
     alert("hi"+marqueediv.innerHTML); 

     // Get the total width of our available scroll area 
     var marqueewidth=document.getElementById("marqueeborder").offsetWidth; 
     alert("marqueewidth"+marqueewidth); 
     // Get the width of the content we want to scroll 
     var contentwidth=marqueediv.offsetWidth; 
     alert("contentwidth"+contentwidth); 
     // Start the ticker at 50 milliseconds per tick, adjust this to suit your preferences 
     // Be warned, setting this lower has heavy impact on client-side CPU usage. Be gentle. 
     var lefttime=setInterval("scrollmarquee()",50); 
     alert("lefttime"+lefttime); 
    } 

    function scrollmarquee(){ 
     // Check position of the div, then shift it left by the set amount of pixels. 
     if (parseInt(marqueediv.style.left)>(contentwidth*(-1))) 
      marqueediv.style.left=parseInt(marqueediv.style.left)-pxptick+"px"; 
     // If it's at the end, move it back to the right. 
     else 
      marqueediv.style.left=parseInt(marqueewidth)+"px"; 
    } 

    window.onload=startmarquee(); 

</script> 

IAM時運行在服務器上的上面的代碼,得到的IAM JavaScript錯誤爲 「對象所需」 在線46還警告( 「marqueediv」 + marqueediv);是「marqueedivnull」之後,警報IAM得到的JavaScript錯誤。

這裏我的問題是,div標籤沒有被識別?爲什麼? 所以只有它變成空對象,我該如何解決這個問題?

謝謝。

+1

'window.onload = startmarquee();'這會立即調用startmarquee函數並將返回值設置爲'window.onload'。你需要傳遞函數的參考。做到這一點,'window.onload = startmarquee'。另外,我建議你使用'console.log'來代替警報。這將有助於讓你的js編程更好:) – Jashwant

回答

2

您立即致電startmarquee並試圖將其返回值(undefined)指定爲window.onload

推測該腳本出現在<head>,並且這個div在您運行時不存在。

將函數指定爲onload,而不是其返回值。

window.onload=startmarquee; 
0

你可以複製腳本和身體的結束標記之前把它和刪除window.onload=startmarquee();從而確保所有的元素都被加載並訪問

就像@Quentin說,用id該元素可能不在你引用它時已經加載到DOM中