2013-11-28 108 views
4

我想在PHP中創建一個類似於JavaScript的alert()命令的函數,但是當我在一個警報上單擊確定按鈕時,所有警報消失!如何獲取元素的父級?

這裏是我的代碼:

<!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>'); 
    alert('Testg', 'Tesng <em>tting</em><b>23</b>'); 
    alert('Tsting', 'ing <em>tng</em><b>123</b>'); 
    alert('Testg', 'Teng <em>tesng</em><b>123</b>'); 
    alert('Teing', 'Teing <em>teng</em><b>123</b>'); 
?> 

我試着用this,但只取得了按鈕消失!我需要找到父元素。我怎麼做?

+0

的ID必須是唯一的,所以你不能有'ID = 「警報」'多個div。 – Barmar

+0

你實際上可以,但是如果你試圖用一個做一些事情,它們都會受到影響。 –

+0

取決於你在做什麼。我認爲jQuery假設一個ID選擇器只會返回一個元素,並在發現它時停止。 – Barmar

回答

2

使用普通的JavaScript與parentNode

element.parentNode 

在jQuery中:

element.parent() 
2

使用this.parentNode取得父母。