如何使用 Javascript 阻止 HTML 元素淡出?

How to stop a HTML element fading out using Javascript?

更新 我有一个 jsfiddle 显示了这里的问题:http://jsfiddle.net/waf11s6u/1/ 当您在搜索栏中键入一个字母时,附加到 div 的自定义滚动条消失了。滚动条可能会被淡出 div?

中不匹配的单词的代码淡出

~~

我正在为 Facebook 游戏创建自定义多好友选择器,它看起来类似于:http://tinyurl.com/gus79cf 用户可以在搜索栏中键入内容,任何匹配的好友名称都会出现在下方区域。 我正在使用自定义滚动条插件来设计用于向下滚动好友列表的滚动条。 这是插件的站点:http://manos.malihu.gr/jquery-custom-content-scroller/

从视觉上看,滚动条由两部分组成,第一部分是轨道(我已将轨道绘制到背景图像上,因此它实际上不是 Javascript 代码的一部分),第二部分部分是图标,图标是沿着轨迹上下移动的小图

滚动条完美运行(意味着图标可以正确上下滑动),除了一件事,每当用户在搜索栏中键入一个字母时,图标就会消失,并且只有在搜索栏时它才会再次可见是空的。

包含朋友姓名和图像的div是在Javascript中动态创建的(称为"mfsForm")。当用户开始输入姓名时,我有一些 Javascript 会淡出不匹配的朋友姓名和图像。

我认为这段代码也会导致图标消失。

这是有问题的代码:

  // Earlier code here connects to Facebook's API.
  // Then get the list of friends for this user with the Graph API
            FB.api('/me/invitable_friends?limit=48', function(response) {
                var container = document.getElementById('mfs');
  // Creating the div "mfsForm" (this will hold the friend names & photos, and is also what the custom scrollbar is applied to.)
                    var mfsForm = document.createElement('form');
                    mfsForm.id = 'mfsForm';
                    mfsForm.className = " mCustomScrollbar mfsForm";

                // Iterate through the array of friends object and create a checkbox for each one.
                for (var i = 0; i < response.data.length; i++) { //Math.min(response.data.length, 10)

                    var friendItem = document.createElement('div');  
                    friendItem.id = 'friend_' + response.data[i].id;
                    friendItem.style.cssText="width:100px; height:100px; padding:7px; color:#FFF;"
                    friendItem.style.cssFloat="left";
                    friendItem.innerHTML = '<input type="checkbox" name="friends" value="' + response.data[i].id + '" />';

                    var img = document.createElement('img');
                    img.src = response.data[i].picture.data.url;
                    img.style.cssText = 'width: 70px;height: 70px;'
                    friendItem.appendChild(img);

                    var labelName = document.createElement('label');
                    labelName.style.cssText = 'font-size: 14px;'
                    labelName.innerHTML = response.data[i].name;
                    friendItem.appendChild(labelName);

                    mfsForm.appendChild(friendItem);
                }
                container.appendChild(mfsForm);
                console.log(mfsForm);               
                $(mfsForm).mCustomScrollbar();

                // Create a button to send the Request(s)
                var sendButton = document.createElement('div');
                sendButton.id = 'sendButton';
                sendButton.onclick = sendRequest;
                container.appendChild(sendButton);

                $("#filter").keyup(function(){

        // Retrieve the input field text and reset the count to zero
        var filter = $(this).val()//, count = 0;

        // Loop through the comment list
        $("#mfsForm div").each(function(){

            // If the list item does not contain the text phrase fade it out
            if ($(this).text().search(new RegExp(filter, "i")) < 0) {
                $(this).fadeOut("slow");


            // Show the list item if the phrase matches and increase the count by 1
            } else {
                $(this).show();

                //Attempting to fade in the icon here:
                $(this).next('.mCSB_dragger_bar').fadeIn("slow"); 

                }
            });
        })
                });

我认为 $(this).fadeOut("slow"); 使滚动条图标淡出。我试图通过引用其 class (mCSB_dragger_bar) 并将其淡入此处来定位该图标: $(this).next('.mCSB_dragger_bar').fadeIn("slow"); 但它不起作用。

任何有关我可以尝试解决此问题的帮助或建议将不胜感激,提前致谢!

问题是什么? 您没有显示正常代码来查看脚本删除图标的位置,我可以说您强制脚本显示此图标。 放置以输入代码 onchange="f()" 或 onkey pres 或其他。 并且

<script>
function f(){ //$('#icon') the element witch contain icon that disapear
$('#icon').css('visibility','visible').css('display','block');
$('#icon').attr('background','url('/icon.png')')}`
/*$('#parent-of-icon').appendChild(icon );*/

其他取决于图标消失的原因。
可能是您的脚本删除图标(html 元素)然后创建它。

在此模式下,图标将始终出现在每次按键时。

尝试$(this).find('.mCSB_dragger_bar').fadeIn("slow");而不是$(this).next('.mCSB_dragger_bar').fadeIn("slow");

如果具有 class 名称 mCSB_dragger_bar 的元素存在于 $(this) 元素上 ( $this -> $("#mfsForm div") -> some div 在 id=mfsForm 的元素上)它会找到它并显示;

NEXT return $this 之后只有一个元素,可能在 $(this) 和 mCSB_dragger_bar 之间存在另一个元素。

如果 mCSB_dragger_bar 和 $(this) 处于相同的厄运级别,也尝试 $(this).parent().find('.mCSB_dragger_bar').fadeIn("slow");