Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new with Javascript and Jquery and I'm facing a small problem.(我是Javascript和Jquery的新手,现在遇到了一个小问题。)

I'm trying to make sure that if a given link exists, hovering over this link will bring up a popup with the fadeToggle().(我试图确保如果存在给定的链接,将鼠标悬停在此链接上将弹出带有fadeToggle()的弹出窗口。) So I wrote this code that works:(所以我写了下面的代码:) if ($('.link-1')) { $('.link-1').mouseover(function () { $('.popup-1').fadeToggle(); }) .mouseout(function () { $('.popup-1').fadeToggle(); }) } But, instead of repeating it ten times, I wanted to write a loop, like this:(但是,我不想重复十次,而是想编写一个循环,如下所示:) var number = 0; while (number < 10) { var popup = '.popup-' + number; var link = '.link-' + number; if ($(link)) { $(link).mouseover(function () { $(popup).fadeToggle(); }) .mouseout(function () { $(popup).fadeToggle(); }) } number++; } But it does not work.(但这行不通。) Could you help me please ?(请问你能帮帮我吗 ?) I thank you in advance !(我提前谢谢你 !)   ask by innocent-musica translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
342 views
Welcome To Ask or Share your Answers For Others

1 Answer

welcome to the web community :-)(欢迎来到网络社区:-))

My jQuery skills are a bit rusty, but I recall, that there is an Attribute Contains Selector , which you could combine with .each() like so:(我的jQuery技巧有点生锈,但是我记得,有一个Attributes Contains Selector ,您可以将它与.each()结合使用, 如下所示:) $('[class*="link-"]').each(function (index, link) { $('[class="popup-"' + index + '"]').each(function (_, popup) { $(link) .mouseover(function () { $(popup).fadeToggle(); }) .mouseout(function () { $(popup).fadeToggle(); }) } } The second index is not interesting, that's why I named the argument ?_”.(第二个索引并不有趣,这就是为什么我将参数命名为“ _”。) Let me know, whether it still works(让我知道,是否仍然有效)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...