javascript - 如何获取元素的父元素?

标签 javascript php html

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

最佳答案

将纯 javascript 与 parentNode 结合使用:

element.parentNode

在 jQuery 中:

element.parent()

关于javascript - 如何获取元素的父元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272026/

相关文章:

javascript - 删除可拖动的包含

javascript - 根据下拉列表和输入字段搜索数据表

php - 在Mysql中创建搜索表单并添加到Joomla首页

php - 如何找到不在这个数组中的所有记录? (MySql)

javascript - 验证数据无效后如何提交表单?

javascript - 检测浏览器缩放级别

javascript - 重定向到错误页面 ReactJS 和 react-router

javascript - 如何在每一行前面加上 `>` 前缀

php - 如何为 LAMP 删除 WIMP

php - 坐标的正则表达式 [0 :0:0] in in square brackets