首页 > 解决方案 > 如何查找单击了哪个“a”html元素

问题描述

这个 php 文件查找每个“.html”文件和“.txt”文件,并创建一个动态表,其中包含指向另一个页面的链接。在那个页面中,我想在一个框架中显示单击的 html 文件。

 <html>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <body>

        <?php

        echo '<h2>Escolha o Esquema de MetaDados a utilizar</h2>';
        echo '<table cellspacing="0" cellpadding="0" border="0" class="table">';
        echo '<tr>';
        echo '<th>Esquema de MetaDados</th>';
        echo '<th>Descrição</th>';
        echo '</tr>';

              foreach (glob("Templates/Dinamico/*.html") as $filename) {
              foreach(glob("Templates/Dinamico/*.txt") as $txtname){ 
               $fileH= basename($filename,".html");
               $fileT= basename($txtname,".txt");   
              if($fileH==$fileT){ 
              $txt= file_get_contents ( $txtname); 
            if($filename != "Templates/Dinamico/formD1.html"){ 
                echo'<tr>';
                echo'<td>';
                echo "<a name='".$fileH."'strong href='Templates/Dinamico/formD1.php'>".$fileH."</a>";
                echo'</td>';
                echo'<td>';
                echo $txt;
                echo'</td>';
                echo'</tr>';               
    }
    }
    }
    }
        ?>
    </body>

    </html>

该文件是上面设置的链接页面,单击的 a 元素会填充 iframe,并将名称分配给“a”元素。

  <html>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <body>
        <div>

        <?php

This $a var i want to be the $fileH clicked on

              echo '<iframe src="' .$a.'" frameBorder="0" width="70%" height="100%" align="left" scrolling="no" />';
              echo '</iframe>'
        ?>
    </div>

    </body>

    </html>

标签: phphtml

解决方案


您可以将 id 作为属性添加到该 url,如下所示:

echo "<a name='".$fileH."'strong href='Templates/Dinamico/formD1.php?id=$fileH'>".$fileH."</a>";

然后在您的其他页面中只需获取 id 就可以了$_GET['id'];。在此处找到有关如何传递变量的更多参考。链接1 、链接 2链接 3


推荐阅读