简体   繁体   English

Javascript(jQuery)Flasher效果:该怎么做?

[英]Javascript (jQuery) flasher effect: how to do that?

I want to make a flasher effect: when hover on some specified field there shows anything (yellow icon in my case) and when mouseout it disappeares. 我想产生闪光效果:将鼠标悬停在某个指定字段上时会显示任何内容(在我的情况下为黄色图标),而当鼠标移开时它会消失。

The problem that there are a lot of such similar fields (they will be the comments in future) so I've tried to do all this in one for cycle. 问题是有很多类似的字段(将来会出现注释),所以我尝试将所有这些都放在一个循环中。 But it's not working... I do not know why. 但这不起作用...我不知道为什么。

Some notes: I have to do that all without html, only in javascript, so I've used document.write method. 一些注意事项:我必须在没有html的情况下执行所有操作,而只能在javascript中进行操作,因此我使用了document.write方法。

Demo on jsfiddle jsfiddle上的演示

Here the code: 这里的代码:

var data = ['one', 'two', 'three'];
for(var i in data){
    (function(){
        $('#id'+i).mouseover(function(){    $("#hdn"+i).show();   });
        $('#id'+i).mouseout(function(){    $("#hdn"+i).hide();   });
    });
    document.write('<div id="id'+i+'" style="border:1px solid;margin:10px 0;float:left;width:100%;">');
        document.write('<div style="float:left;width:20px;height:20px;">'+data[i]+'</div>');
        document.write('<div id="hdn'+i+'" style="display:none;float:right;width:20px;height:20px;"><img src="http://t0.gstatic.com/images?q=tbn:ANd9GcSa7ebCG4VGWcTTXH8j7ebfpFWhYuV9ojisNmsrnZaQHk8wRMTNqGfaNA" /></div>')
    document.write('</div>');
}

Thanks! 谢谢!

You're setting the event handlers before you're writing out the html. 您需要在写出html 之前设置事件处理程序。 That can't work. 那行不通。

Also, for in loops in JavaScript give you the index of the array, not the actual value. 另外,JavaScript中的for循环为您提供数组的索引,而不是实际值。

The best way to do this would be to set up a div for the html, add this html to it, then have jQuery add the div to your body. 最好的方法是为html设置div,将此html添加到其中,然后让jQuery将div添加到您的身体。

var newHtml = "";
for(var i = 0; i < data.length; i++) {
    newHtml += '<div id="id'+i+'" style="border:1px solid;margin:10px 0;float:left;width:100%;">';
        newHtml += '<div style="float:left;width:20px;height:20px;">'+data[i]+'</div>';
        newHtml += '<div id="hdn'+i+'" style="display:none;float:right;width:20px;height:20px;"><img src="http://t0.gstatic.com/images?q=tbn:ANd9GcSa7ebCG4VGWcTTXH8j7ebfpFWhYuV9ojisNmsrnZaQHk8wRMTNqGfaNA" /></div>';
    newHtml += '</div>';
}

var newDiv = $("<div />");
$("body").append(newDiv);
$(newDiv).html(newHtml);

for(var i = 0; i < data.length; i++) {
    (function(){
        $('#id'+i).mouseover(function(){    $("#hdn"+i).show();   });
        $('#id'+i).mouseout(function(){    $("#hdn"+i).hide();   });
    });
var data = ['one', 'two', 'three'];

$.each(data,function(i,v) {
    $('<div id="id'+i+'" style="border:1px solid;margin:10px 0;float:left;width:100%;">')
        .append('<div style="float:left;width:20px;height:20px;">'
                 +v+'</div><div id="hdn'
                 +i+'" style="display:none;float:right;width:20px;height:20px;"><img src="http://t0.gstatic.com/images?q=tbn:ANd9GcSa7ebCG4VGWcTTXH8j7ebfpFWhYuV9ojisNmsrnZaQHk8wRMTNqGfaNA" /></div>')
        .appendTo('body')
        .mouseover(function(){    $(this).children('[id^="hdn"]').show();   })
        .mouseout(function(){    $(this).children('[id^="hdn"]').hide();   });
});

If you use class names instead of IDs you can affect multiple elements in one go. 如果使用类名而不是ID,则可以一口气影响多个元素。 Also, don't use document.write , update the innerHTML of an existing element instead. 另外,不要使用document.write ,而是更新现有元素的innerHTML。

$('#outputDIV').html("....")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM