0

我怎么把它放在一起?我正在这个网站上工作,该网站要求每次单击菜单中的一个项目时,两个特定 DIV 的背景位置都会更改并保留选择。

这是我的尝试:

首先,我创建一个变量“p”来保存所选页面的 url/地址,例如,如果您在 about us,您可能会在地址栏上看到类似这样的内容:

website.com/index.php?p=aboutus

在菜单上,在关于我们的按钮上,我通过调用函数 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 回答