Javascript 如何在jQuery中使用多个选择器?

Javascript 如何在jQuery中使用多个选择器?,javascript,jquery,Javascript,Jquery,我正在使用以下代码对名为toolbar的div中的一组图像应用悬停效果: $("#toolbar img").hover( function() { this.src = this.src.replace("_off", "_on"); }, function() { this.src = this.src.replace("_on", "_off"); } ); 但我也希望对名为tabs的div应用相同的效果,而

我正在使用以下代码对名为toolbar的div中的一组图像应用悬停效果:

$("#toolbar img").hover(
    function()
    {
        this.src = this.src.replace("_off", "_on");
    },
    function()
    {
        this.src = this.src.replace("_on", "_off");
    }
);
但我也希望对名为tabs的div应用相同的效果,而不重复相同的代码。我不能按照这些思路做点什么吗:

$("#toolbar, #tabs img").hover(
    function()
    {
        this.src = this.src.replace("_off", "_on");
    },
    function()
    {
        this.src = this.src.replace("_on", "_off");
    }
);

问题是上面的方法只适用于“tabs”div,而工具栏停止工作。我想我只是把jQuery语法弄错了,对jQuery来说还是有点陌生。如果要按照第一个示例中的效果重新创建效果,则需要:

$("#toolbar img, #tabs img")
“工具栏img”是整个选择器。它说“将其应用于id为toolbar的父元素中的任何img标记”。例如:


#tabs img是一个完整的其他选择器

这些与CSS选择器的语法相同,因此要了解更多信息,请尝试使用“#toolbar img,#tabs img”(“#toolbar img,#tabs img”)是否应该使用“#toolbar img,#tabs”而不是“#toolbar,#tabs img”

<div id ="toolbar">
   <img .../>
   <img .../>
</div>