确定哪个元素引用了当前html

Determining which element referred current html

本文关键字:html 引用 元素      更新时间:2023-09-26

我有一个有两个按钮的HTML页面。这两个按钮都指向同一个页面,比如Test2。在开发Test2时(创建新元素等)我需要知道哪个按钮被点击了。下面是一个示例代码:

HTML1:

<head>
<title>Test 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div class="Main" id="Main">
<div id="Div1"> 
<button id="Btn1"  onclick="window.location.href='Test2.html';"> this is Button 1 </button>
</div>
<div id="Div2"> 
<button id="Btn2"  onclick="window.location.href='Test2.html';"> this is Button 2 </button>
</div>
</div>
</body>
</html>

HTML2:

    <head>
    <title>Test 2</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <script type='text/javascript'>
if (1==1) { //if something happened
    var aaa=window.document.referrer.source;
    alert(aaa)}
    </script>
    </body>
    </html>

此代码产生"undefined"警告消息。我如何得到指向test2。html的按钮或div的id ?

考虑到使用onclick来设置新位置,我将为每个URL添加一个查询参数:

<button … onclick=onclick="window.location.href='Test2.html?btn=1';" … >#

然后解析Test2.html代码中的查询字符串