【问题标题】:Jquery CSS for inner div that has overridden color for opacity用于覆盖不透明度颜色的内部 div 的 Jquery CSS
【发布时间】:2018-03-24 22:58:51
【问题描述】:

我有这个 html:

$('.allOfMyElementsLikeThisByClass').hover(inFunction, outFunction);

function inFunction() {
  setTimeout(function() {
    $(this).children('.nshow').css("color", "rgba(146,151,154, 1)");
  }, 200);
}
function outFunction() {
 $(this).children('.nshow').css("color", "rgba(146,151,154,0)");
}
.nshow {
  color: rgba(148, 51, 54, 0);
}

.alwaysshow {
  color: rgba(148, 51, 54, 1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='allOfMyElementsLikeThisByClass'>
  <div class='nshow'>
    <div class='alwaysshow'>This should always show</div>
    , but this should show up when I want.
  </div>
</div>

如果我直接调用该类,它将正确运行并设置颜色值。如果我为了简单起见尝试相对引用它(就像我做过很多次一样),它将无法正确引用。但是,它将完全正确地用于不透明度。我这样做的方式有问题吗?

此外,如果没有周围的 setTimeout 函数,这完全可以正常工作,所以我知道我引用选择器的方式不是问题。此外,事先将其分成变量并不能解决问题,但也可以在没有 setTimeout 函数的情况下工作。 setInverval 有同样的问题。

【问题讨论】:

  • 更新:似乎可以在 Firefox 中使用。仅在 Chrome 中存在问题。
  • outFunction在哪里?
  • 请将outFunction添加到您的sn-p。
  • 您能解释一下setTimeout() 的用途吗?这可能会导致您的 inFunction 在您的 outFunction 之后运行。
  • 另外,如果$(this) 确实包含您所期望的内容,我会感到惊讶。

标签: javascript jquery html css settimeout


【解决方案1】:

将 setTimeout() 函数移除到它自己的“命名空间”允许执行远离悬停事件,从而允许执行 setTimout() 函数:

 function st(event){
                 setTimeout(function(){
                          $(event).attr("style" , "color: rgba(148,51,54, 1)"); 
                        }, 2000);

             }   

function inFunction(){
var event = $(this).children(".nshow");
st(event);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2016-09-28
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多