0

どうすればこれをまとめることができますか? メニューの項目がクリックされるたびに、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>

なぜこれがうまくいかないのか誰にもわかりますか?

4

1 に答える 1

0

引用符を使って試してみてください: style.backgroundPosition="0 20px";

更新しました

また、次の行はどうですか: pvalue = '';

pvalue='=aboutus' を設定しています

更新しました

スクリプト要素が正しくありません:

<script type="java/text">

右は:

<script type="text/javascript">

さらに、firebug または chrome を使用して、ページで生成された JS を確認します。

于 2012-01-02T20:32:28.893 に答える