我想在PHP中創建一個類似於JavaScript的alert()命令的函數,但是OK按鈕的onclick屬性不起作用!爲什麼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>');
?>
有什麼不對?我收到一個「警告框」,但確定按鈕不起作用!
如果你calilng這多次,你必須與多個div相同的id,並且只有FIRST發現的id會被'document.getElementById()'返回。 –
這是ID選擇器中的報價不匹配! – adeneo
@MarcB提出了一個有效的觀點。如果您在同一頁面上有兩個警報框,它們都會關閉其中的一個,這很奇怪。每次渲染警告框時生成一個唯一的ID。 – Halcyon