1
私は2つのボタンを持つHTMLページを持っています。これらのボタンはどちらも同じページを指しています(Test2など)。 Test2(新しい要素の作成など)に取り組んでいる間に、どのボタンがクリックされたかを知る必要があります。ここでは例のコードは次のとおりです。現在のhtmlを参照している要素を確認する
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>
このコードの結果。 Test2.htmlと呼ばれるボタンやdivのIDを取得するにはどうすればよいですか?
<button … onclick=onclick="window.location.href='Test2.html?btn=1';" … >#
そしてTest2.html
にコード内のクエリ文字列を解析:onclick
の使用はちょうど私が各URLにクエリパラメータを追加する新しい場所を設定するために与えられた
hrefをTest2.html?id = Div1に設定し、それをページ2で取得する方法はいくつかあります。 – juvian
リンク先を 'Test2.html?id = Btn1'にして、' Btn1'をTest2これで:[JavaScriptでクエリ文字列値を取得する方法は?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) –