Javascript 选择此LI项的jquery选择器语法是什么

Javascript 选择此LI项的jquery选择器语法是什么,javascript,jquery,css,Javascript,Jquery,Css,我需要选择每个列表项并更改父div homepagecontainer的背景图像,但我甚至无法在脚本中选择li元素。代码如下: <div class="transparent" id="programmesbox"> <ul id="frontpage"> <?php query_posts('showposts=20&post_parent=7&post_type=page'); if (have_posts()) : while (have

我需要选择每个列表项并更改父div homepagecontainer的背景图像,但我甚至无法在脚本中选择li元素。代码如下:

<div class="transparent" id="programmesbox">
<ul id="frontpage">
<?php 
query_posts('showposts=20&post_parent=7&post_type=page'); 

if (have_posts()) : while (have_posts()) : the_post();
?>

<li id="<?php the_id() ?>" ><a class="sprite" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>

</ul>
</div>
我需要这样做

    <script type="text/javascript"
     src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
    $('#frontpage li a').hover(function() {
    alert('here');
//CHANGE BACKGROUND IMAGE OF 'homepage_container' to different image depending on which list item is hovered over
    }

    );


    </script>
这是网站的URL:-

非常感谢


Jonathan

您的脚本看起来没有错,但看起来好像放错了位置。将其放入以确保加载了所有要访问的元素

$(document).ready(function(){
    $('#frontpage').find('a').hover(function() {
       $(this).closest('.homepage_container').css('background-image', 'some_image_url_here');
    }, function() {
       // mouseleave code here
    }); 
});
在您的示例中,我无法确定主页容器相对于锚的位置,因此我使用了该函数。可以通过更具体的方式进行优化。
如果它是一个ID,你可以使用$'homepagecontainer'.css'background-image','url';由于ID在有效的HTML标记中必须是唯一的。

查看源代码,正文中有两个单独的JS调用,还调用了两次jQuery

我建议您将所有JS放在页面的顶部或底部,只需调用一次jQuery

对于您的li a选择,在进行这些更改后,请尝试此

<script type="text/javascript"> 
$(document).ready(function(){
  $("#contact").click(function(event){
     event.preventDefault(); //stops the browser from following the link

     $("#contactable").click(); //opens contact form
     alert("Showing mycontactform"); //remove this whenit's working
  });

  $("ul#frontpage li a").hover(
   function () {
     $('#homepage_container').css('background-image', 'url(image1.jpg)');
     },function () {
     $('#homepage_container').css('background-image', 'url(image2.jpg)');
     }
    );
});   
</script>

您同时包含了jquery 1.4.1和1.4.2,我强烈建议您删除1.4.1脚本标记:您好,非常感谢-这很有效,我只需要知道如何为每个列表项指定不同的图像,我知道每个列表项的id,这会有帮助吗?如何将其传递给javascript?谢谢好吧,我想出来了——可能不是最优雅的解决方案,但它很管用。我刚刚将id添加到选择器并复制了函数-有人知道如何设置背景图像更改的动画,使其从一个渐变到另一个吗?已经回答了,我想你会得到你需要的,