jquery - jquery 中的简写

标签 jquery

我有这个 jquery 代码:

 $(document).ready(function() {
$("#masthead ul li").mouseover(function(){
    $("#masthead ul li").removeClass("cr");
    $(this).toggleClass("cr");
});
   }); 

  $(document).ready(function() {
$("#intUl li").mouseover(function(){
    $("#intUl li").removeClass("cr");
    $(this).toggleClass("cr");
});
}); 

我们可以为两个相似的 jquery 代码编写简写吗? 提前致谢

最佳答案

$('#masthead ul li, #intUL li').mouseover(function() {
    $(this).siblings().removeClass("cr");
    $(this).toggleClass("cr");  
});

与原始代码相同; 但是,不确定为什么要删除一个类只是为了重新添加它。如果您试图让它闪烁或其他什么,我相当确定它不会像您期望的那样工作。

误解了初衷;评论者为我澄清了,我修复了它:)

关于jquery - jquery 中的简写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2117631/

相关文章:

jquery - 如何从 jquery datepicker 获取选定的日/月和年

jquery 移动滑动页面,用于类似于 Facebook 的设置

javascript - html 不允许我们在 textarea 中选择交替行

javascript - jQuery Tooltip 图像翻转显示更大的图像

javascript - Handlebars 不解析?

javascript - 如何使用类名克隆元素并将其附加到正文

jquery - 垂直滚动可排序,而不是水平滚动,没有静态位置

javascript - 如何在列表中动态生成输入字段,然后从所述字段中检索文本?

javascript - 使用 jQuery 获取 ID 并将它们放入 Anker 导航中

javascript - 更改隐藏值并使用 onclick for href 提交表单