简体   繁体   English

如何为每篇文章应用jquery函数

[英]How to apply jquery function for each article

I have a DOM-like this.我有一个像这样的 DOM。 I have a jquery function that checks if there is a class "visible" into the article, remove it and add it on the clicked "hotspot" class.我有一个 jquery 函数,用于检查文章中是否有“可见”类,将其删除并将其添加到单击的“热点”类中。 This function has to work for-each "article" independently from the others.此功能必须独立于其他“文章”对每个“文章”起作用。 I create this function but it doesn't work:我创建了这个函数,但它不起作用:

 $("article").each(function() { $("article .hotspot").on("touchstart", function() { $(".hotspot.visible").not(this).removeClass('visible'); $(this).toggleClass('visible'); }) })
 <article> <div class="hotspot"> <div class="hotspot"> </article> <article> <div class="hotspot"> <div class="hotspot"> </article>

Do you have any suggestion?你有什么建议吗?

The main problem will be solved if you remove the .each() loop, check the explanation below.如果删除.each()循环,主要问题将得到解决,请查看下面的说明。

Explanation:解释:

Since you loop using the .each() method, in every iteration the event will be attached, in your case you've two article's so the event will be attached twice to every item and then it will be fired two times when trigger touchstart .由于您使用.each()方法进行循环,因此在每次迭代中都会附加事件,在您的情况下,您有两篇article's因此事件将附加到每个项目两次,然后在触发touchstart时触发touchstart

Then because you're using toggleClass() the first event will add or remove the class then the second one will do the opposite and it'll look like the event isn't fired at all.然后,因为您使用的是toggleClass() ,第一个事件将添加或删除该类,然后第二个事件将执行相反的操作,看起来该事件根本没有被触发。

 $("article .hotspot").on("touchstart click", function() { $(this).parent().find(".hotspot.visible").not(this).removeClass('visible'); $(this).toggleClass('visible'); });
 .visible { background-color: green; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <article> <div class="hotspot">1</div> <div class="hotspot">2</div> </article> <br> <article> <div class="hotspot">1</div> <div class="hotspot">2</div> </article>

NOTE: To check the behavior that I've explained you can see the console.log('Event fired') inside the event was triggered two times on click.注意:要检查我已经解释过的行为,您可以看到console.log('Event fired')console.log('Event fired')在单击时触发了两次。

 $("article").each(function() { $("article .hotspot").on("touchstart click", function() { $(".hotspot.visible").not(this).removeClass('visible'); $(this).toggleClass('visible'); console.log('Event fired'); }) })
 .visible { background-color: green; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <article> <div class="hotspot">1</div> <div class="hotspot">2</div> </article> <article> <div class="hotspot">1</div> <div class="hotspot">2</div> </article>

this should work for you这应该适合你

$("article .hotspot").on("touchstart", function() {
    $("article .hotspot").removeClass('visible');
    $(this).addClass('visible');
});

or或者

$("article .hotspot").on("click", function() {
    $("article .hotspot").removeClass('visible');
    $(this).addClass('visible');
});

Here you go:干得好:

 $("article").each(function() { $(this).children('.hotspot').on("touchstart click", function(){ $(this).siblings().removeClass('active'); $(this).addClass('active'); }) })
 article .hotspot.active{ background: red; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <article> <div class="hotspot">1</div> <div class="hotspot">2</div> </article> <br> <article> <div class="hotspot">1</div> <div class="hotspot">2</div> </article>

If you want to have more than one active hotspot selected, then remove:如果您想选择多个活动热点,请删除:

$(this).siblings().removeClass('active');

and change:并改变:

$(this).addClass('active');

for为了

$(this).toggleClass('active');

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

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