drasv0904 2014-12-02 01:57
浏览 59
已采纳

PHP中的Javascript布鲁斯:getElementById和getElementsByClassName

This should be simple enough, alas it's giving me issues:

I have a PHP page that gives me up to three different iterations of td contents. The first is shown with display: table-cell, and the others are hidden with display: none.

echo '<td style="display: table-cell" class="gameLineCell" id="'.$playerLine.'" colspan="3">';

&

echo '<td style="display: none" class="gameLineCell" id="'.$playerLine.'" colspan="3">';

I then have a link corresponding to each td option -- clicking it is supposed to run a function loadLine into which I pass a string that corresponds to the td ID. ID strings akin to 'EV-F1' -- five characters, a number, a hyphen.

echo "<a onclick='loadLine(".$playerLine.")' href='javascript:void(0)'>".$playerLine."</a>";

The script hides all tds of the class gameLineCell, and displays the one whose link was clicked.

<script>
    function loadLine(line) {
        var lines = document.getElementsByClassName('gameLineCell');
        for (var i = 0; i < lines.length; i++) {
            lines[i].style.display = 'none';
        }
            document.getElementById(line).style.display = 'table-cell';
    }
</script>

When I view the source of my PHP page everything renders properly -- my links show the proper strings in the loadLine brackets, each of my tds is present, the first showing and the others hidden -- however my links do not work. I've tried removing the getElementsByClassName sequence, running only the getElementById, though to no avail. Nothing happens.

Any ideas?

Much obliged to any help,

Andrew


EDIT: Source of the error? Javascript is cutting my strings! EV-F1 becomes EV. Looking into the whys and how fix now.

  • 写回答

2条回答 默认 最新

  • doujin8476 2014-12-02 02:08
    关注

    There are a few issues here related to proper escaping and enclosure of strings; I personally use printf() to print HTML, this is what it would look like:

    printf('<td style="display: table-cell" class="gameLineCell" id="%s" colspan="3">',
        htmlspecialchars($playerLine, ENT_QUOTES, 'UTF-8');
    );
    

    This piece of code in particular is tricky:

    echo "<a onclick='loadLine(".$playerLine.")' href='javascript:void(0)'>".$playerLine."</a>";
    

    The problem is that $playerLine doesn't contain JavaScript string enclosures and so it would attempt to resolve EV-F1 into EV - F1 (i.e., the subtraction of EV and F1). To fix that, you need to encode the variable using JSON and then apply HTML escaping:

    printf('<a onclick="loadLine(%s)" href="javascript:void(0)">%s</a>',
        htmlspecialchars(json_encode($playerLine), ENT_QUOTES, 'UTF-8'),
        htmlspecialchars($playerLine), ENT_QUOTES, 'UTF-8')
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?