我如何获得元素的父项? [英] How do I get the parent of an element?

查看:80
本文介绍了我如何获得元素的父项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在PHP中创建一个类似于JavaScript的alert()命令的函数,但是当我在一个警报上单击确定按钮时,所有警报消失!

I am trying to make a function in PHP that is like JavaScript's alert() command, but when I click the OK button on one alert, all of the alerts disappear!

以下是我的代码:

Here is my code:

<!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>');
?>

我试过使用这个,但只使按钮消失!我需要找到父元素。如何做到这一点?

I tried using this, but that only made the button disappear! I need to find the parent element. How do I do that?

推荐答案

使用普通的javascript与 parentNode

Using plain javascript with parentNode:

element.parentNode

在jQuery中:

element.parent()

这篇关于我如何获得元素的父项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆