javascript - 在 jQuery 中链接时 CSS 属性不起作用

标签 javascript jquery css

这是 jQuery 代码:

$("#web").hover(
  function () {
    $(this).css({
             'background-color': '#ecf5fb',
             'cursor': 'pointer',
             'border': '1px solid #378ac4'
           })
           .children("a").children("img").attr("src", "1.png")
           .end().children("p").css("opacity", "1.0");

           $('#commentweb').stop(true, true).fadeIn();
  }, 
  function () {
    $(this).css({
             'background-color': '#e8e3e3',
             'border': '1px solid grey'
             })
             .children("a").children("img").attr("src", "2.png")
             .end().children("p").css("opacity", "0.5");

             $('#commentweb').stop(true, true).fadeOut();
  }
);

问题是不透明度没有改变,而其他一切正常。但是如果我写的不是这段代码

$(this).css({
         'background-color': '#ecf5fb',
     'cursor': 'pointer',
     'border': '1px solid #378ac4'
       })
       .children("a").children("img").attr("src", "1.png");
$(this).children("p").css("opacity", "1.0");

它有效。为什么会这样?

这是 fiddle :http://jsfiddle.net/mMB3F/6/

最佳答案

如果您想回到原来的选择,您必须调用 .end()两次,就像你在链上两次调用 child 一样。

$("#web").hover(
  function () {
    $(this).css({
             'background-color': '#ecf5fb',
             'cursor': 'pointer',
             'border': '1px solid #378ac4'
           })
           .children("a").children("img").attr("src", "1.png")
           .end().end().children("p").css("opacity", "1.0");

           $('#commentweb').stop(true, true).fadeIn();
  }, 
  function () {
    $(this).css({
             'background-color': '#e8e3e3',
             'border': '1px solid grey'
             })
             .children("a").children("img").attr("src", "2.png")
             .end().end().children("p").css("opacity", "0.5");

             $('#commentweb').stop(true, true).fadeOut();
  }
);

http://jsfiddle.net/mMB3F/8/

关于javascript - 在 jQuery 中链接时 CSS 属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12128550/

相关文章:

javascript - Yii+datepicker boostrap 如何更新网格

javascript - Node.js 用递归函数以某种方式改变了我的对象

php - 当 Ajax 建表时,如何根据其中的数据量有条件地设置 <td> 的高度?

php - Magento中调用第二个图片URL实现鼠标悬停换图功能

javascript - 如果键等于变量,如何显示值

javascript - 在 Windows 上运行 .js javascript 文件的最简单方法是什么?

javascript - 遍历对象中的 JavaScript 数组以在 HTML 中显示

jquery - 列出一条记录自动完成

javascript - 有没有办法在月 View 中向 Kendo Scheduler 添加 'X more events' 样式的消息?

jquery - 如何拖放jquery?