douju2053 2012-06-06 05:56
浏览 37
已采纳

获取所需的javascript错误对象

Iam getting offsetwidth of an div tag. Below is code.

 <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>
";
        // 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>

below is the javascript function which will be called onlaod of the page.

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

when iam running the above code on server, iam getting javascript error as "object required" at line 46 also the alert("marqueediv"+marqueediv); is "marqueedivnull" after that alert iam getting the javascript error.

Here my question is, did the div tag is not getting recognized?why? so that only it is getting as null object, how can i resolved this?

Thanks.

  • 写回答

2条回答 默认 最新

  • dongpao1905 2012-06-06 06:01
    关注

    You are calling startmarquee immediately and trying to assign its return value (undefined) to window.onload.

    Presumably the script appears in the <head> and this the div does not exist at the time you run it.

    Assign the function to onload, not its return value.

    window.onload=startmarquee;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法