php - 获取所需的 javascript 错误对象

标签 php javascript

我正在获取 div 标签的 offsetwidth。下面是代码。

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

下面是 javascript 函数,它将在页面的 onlaod 中调用。

<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 在服务器上运行上述代码时,我在第 46 行收到 javascript 错误“需要对象”,同时还出现警报(“marqueediv”+marqueediv);是“marqueedivnull”之后的警报我得到了javascript错误。

我的问题是,div 标签没有被识别吗?为什么? 这样只有它成为空对象,我该如何解决这个问题?

谢谢。

最佳答案

您正在调用startmarquee立即并尝试将其返回值( undefined )分配给 window.onload .

推测该脚本出现在<head>中并且这个 div 在您运行它时并不存在。

将函数分配给onload ,而不是它的返回值。

window.onload=startmarquee;

关于php - 获取所需的 javascript 错误对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10908839/

相关文章:

javascript - Webpack 插件 - 何时调用 doResolve 以及何时回调?

javascript - 如何在 xhr 对象上设置 getAllResponseHeaders()

javascript - jQuery 图片幻灯片不工作?

php - 服务器上的 http header 请求超时

php - SYMFONY 4 - 如何将用户重定向到同一表单的下一部分? (在另一页)

javascript - 多提交按钮多复选框无表单jquery ajax php提交

java - 我的代码得到 "<identifier> expected",但我不明白为什么我的代码不起作用

php - 解析错误 : syntax error, 意外 '<' ,预计第 23 行 C :\xampp\htdocs\SLR\CreateData2. php 中的文件结尾

javascript - 在 Javascript 中,如何使用 '/' 和 '/g' 内的变量对字符串执行全局替换?

javascript - 'production' 代码中不推荐使用 jQuery .css