为什么onclick属性不起作用?

时间:2013-11-28 17:20:16

标签: javascript php html

我正在尝试在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>');
?>

有什么问题?我得到一个“警告框”但是OK按钮不起作用!

2 个答案:

答案 0 :(得分:2)

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

ps只想清除javascript概念

答案 1 :(得分:1)

更改为:

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

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

看看你是否能找到合适的方法;)(提示:你可以)。

相关问题