2012-06-06 2 views
0

div 태그의 크기가 벗어납니다. 아래는 코드입니다.자바 스크립트 오류 개체가 필요합니다.

<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 기능입니다. 스피가 서버에서 위의 코드를 실행, 스피가 같은 자바 스크립트 오류도 라인 (46) 경고에서 "개체 필요"하기

<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> 

("marqueediv"+ marqueediv); 해당 경고 후 자바 스크립트 오류가 "marqueedivnull"입니다.

제 질문은 div 태그가 인식되지 않았습니까? 왜? null 객체로만 가져 오도록하려면이 문제를 어떻게 해결할 수 있습니까?

감사합니다.

+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에로드되었습니다.