どうすればこれをまとめることができますか? メニューの項目がクリックされるたびに、2 つの特定の DIV の背景位置が変更され、選択を維持する必要があるこの Web サイトに取り組んでいます。
これが私の試みです:
最初に、変数「p」を作成して、選択したページの URL/アドレスを保存します。たとえば、私たちについている場合は、アドレス バーに次のようなものが表示されるでしょう。
website.com/index.php?p=aboutus
メニューの [about us] ボタンで、関数 changeBG onClick を呼び出してこれを行いました。
<a href="?p=aboutus" onclick="changeBG()">About Us</a>
トリックを行うにはjavascriptが必要だと思いました。ここに在庫があります。
これは、ヘッダーに入るスクリプトです。
<script type="text/javascript">
<?
// Set "p" value to HOME if previously empty.
if ($_GET['p']=='') $_GET['p']='home';
?>
function changeBg(pvalue) {
if (pvalue == '') {pvalue = '<?=$_GET['p'];?>';
}
if (pvalue=='aboutus'){
document.getElementById('this_is_the_first_to_be_changed').style.backgroundPosition=0 20px;
document.getElementById('this_is_the_other_to_be_changed').style.backgroundPosition=0 80px;
if (pvalue=='contactus'){
document.getElementById('this_is_the_first_to_be_changed').style.backgroundPosition=0 10px;
document.getElementById('this_is_the_other_to_be_changed').style.backgroundPosition=0 100px;
}
}
</script>
これが体に起こることです:
<div id="left_menu">
<a href="?p=aboutus" onclick="changeBG()">About Us</a>
<a href="?p=contact" onclick="changeBG()">Contact Us</a>
</div>
<div id="this_is_the_first_to_be_changed">
</div>
<div id="this_is_the_other_to_be_changed">
</div>
なぜこれがうまくいかないのか誰にもわかりますか?