如何获取元素的父级


How do I get the parent of an element?

我正试图在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,但那只会让按钮消失!我需要找到父元素。我该怎么做?

在parentNode:中使用纯javascript

element.parentNode

在jQuery中:

element.parent()

使用this.parentNode获取父级。