为什么onclick属性不起作用


Why is the onclick attribute not working?

我正试图在PHP中创建一个类似于JavaScript的alert()命令的函数,但OK按钮的onclick属性不起作用!

这是我的代码:

<!DOCTYPE html>
<?php
    function alert($title, $text) {
        $html = '<div id="alert" style="background-color:lightGray; text-align:center; height:500px; width:500px; color: black; position:fixed; top: 50%; left:50%; margin-left:-250px; margin-top:-250px;">';
        $html = $html . '<h1 style="background-color:red; border-radius: 15px;">'. $title . '</h1>' . $text;
        $html = $html . '<br><br><button type="button" style="border-radius:25px; height:50px; width:100px; background-color:white; border:none;" onclick="document.getElementById('"alert'").style.display=none">OK</button>';
        echo $html;
    }
    alert('Testing', 'Testing <em>testing</em><b>123</b>');
?>

怎么了?我收到一个"警报框",但"确定"按钮不起作用!

element.style.display='none'而不是none

ps只是想清楚javascript概念

更改为:

document.getElementById('"alert'").style.display='"none'"

我想这也表明你目前的做法相当糟糕。必须在没有代码突出显示的情况下添加所有这些转义符是令人讨厌的。

看看你是否能找到一种正确的方法;)(提示:你可以)。