将javascript插入echo PHP

时间:2015-12-19 15:15:45

标签: javascript php html5 audio

我正在尝试将一些音频文件调用到我的脚本中,我只想显示一个带有选项play的按钮,而不是完整的音频控制器html5。

    for ($i=0; $i < 10; $i++) {
           echo "<div class='slide bit" . $i . " rest-slide images-slides'>";
                 $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                 echo "<div class='row'>";
                    printf ("%s \n", $row["name"]);
                 echo "</div>";
                 echo "<div class='row'>";
                    echo "<img src='media/bits/" . $row["file"] . "'>";
                 echo "</div>";
                 echo "<div class='row'>";
                    echo "<audio id='player' src='media/audios/"  . $row["audio"] .  "'></audio>";
                    echo "<button onclick='document.getElementById('player').play();'>Reproducir</button>";
                 echo "</div>";
           echo "</div>";
}

在网络上,代码就像这样,我无法听取音频,

<button onclick="document.getElementById(" player').play();'="">Reproducir</button>

有什么建议吗?

谢谢大家。

@ lex82求解

现在的代码是这样的,它工作正常,

for ($i=0; $i < 10; $i++) {
           echo "<div class='slide bit" . $i . " rest-slide images-slides'>";
                 $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                 echo "<div class='row'>";
                    printf ("%s \n", $row["name"]);
                 echo "</div>";
                 echo "<div class='row'>";
                    echo "<img src='media/bits/" . $row["file"] . "'>";
                 echo "</div>";
                 echo "<div class='row'>";
                    $id = $row["name"];
                    echo "<audio id=". $id . " src='media/audios/"  . $row["audio"] .  "'></audio>";
                    echo "<button onclick=\"document.getElementById('". $id . "').play()\">Reproducir</button>";
                 echo "</div>";
           echo "</div>";
        }

1 个答案:

答案 0 :(得分:2)

逃避这样的引号:

echo "<button onclick=\"document.getElementById('player').play()\">Reproducir</button>";