admin管理员组文章数量:1290270
I have a star rating system as defined like this:
<span class="rating_container">
<span class="star_container">
<a rel="nofollow" href="" class="star star_1" >1<span class="rating">Terrible</span></a>
<a rel="nofollow" href="" class="star star_2" >2<span class="rating">Bad</span></a>
<a rel="nofollow" href="" class="star star_3" >3<span class="rating">Bad</span></a>
<a rel="nofollow" href="" class="star star_4" >4<span class="rating">OK</span></a>
<a rel="nofollow" href="" class="star star_5" >5<span class="rating">OK</span></a>
<a rel="nofollow" href="" class="star star_6" >6<span class="rating">OK</span></a>
<a rel="nofollow" href="" class="star star_7" >7<span class="rating">Good</span></a>
<a rel="nofollow" href="" class="star star_8" >8<span class="rating">Good</span></a>
<a rel="nofollow" href="" class="star star_9" >9<span class="rating">Excellent</span></a>
<a rel="nofollow" href="" class="star star_10" >10<span class="rating">Excellent</span></a>
</span>
</span>
Each individual star is colored when a mouseover happens. How can I simulate this with jquery? For instance I'd like to mouseover star 5. This is what I've tried:
$('.star.star_5').addClass('active');
What am I missing?
I have a star rating system as defined like this:
<span class="rating_container">
<span class="star_container">
<a rel="nofollow" href="" class="star star_1" >1<span class="rating">Terrible</span></a>
<a rel="nofollow" href="" class="star star_2" >2<span class="rating">Bad</span></a>
<a rel="nofollow" href="" class="star star_3" >3<span class="rating">Bad</span></a>
<a rel="nofollow" href="" class="star star_4" >4<span class="rating">OK</span></a>
<a rel="nofollow" href="" class="star star_5" >5<span class="rating">OK</span></a>
<a rel="nofollow" href="" class="star star_6" >6<span class="rating">OK</span></a>
<a rel="nofollow" href="" class="star star_7" >7<span class="rating">Good</span></a>
<a rel="nofollow" href="" class="star star_8" >8<span class="rating">Good</span></a>
<a rel="nofollow" href="" class="star star_9" >9<span class="rating">Excellent</span></a>
<a rel="nofollow" href="" class="star star_10" >10<span class="rating">Excellent</span></a>
</span>
</span>
Each individual star is colored when a mouseover happens. How can I simulate this with jquery? For instance I'd like to mouseover star 5. This is what I've tried:
$('.star.star_5').addClass('active');
What am I missing?
Share Improve this question edited Jan 9, 2013 at 22:04 Paul asked Jan 9, 2013 at 21:09 PaulPaul 11.8k32 gold badges92 silver badges145 bronze badges 4- Gonna point out the obvious but if the class .active doesnt have the styles to light up that star then even correcting the selector format will still not work. And there's no :hover psuedo event that will remain triggered (mouseover is a one shot). I'd need to see your css styles for the star to see what you're doing. – Syon Commented Jan 9, 2013 at 21:25
- Here's an example of what I think you're trying to acplish. I hope this is helpful, if so I can post an answer later when I return; if not just ignore :) and good luck. jsfiddle/Z8d95 (using add/remove classes to simulate mouseover and not :hover) – Syon Commented Jan 9, 2013 at 21:46
-
You don't have a class 'active' defined. Try using "hovered" instead.
$('.star.star_5').addClass('hovered');
– IanW Commented Jan 9, 2013 at 22:02 - @Paul Was my answer the one that worked? If so I'll post as an actual answer so you can accept. – Syon Commented Jan 10, 2013 at 1:25
4 Answers
Reset to default 5Try :
$(".star_5").trigger('mouseover');
This will trigger the mouseover action whatever it happens to be, rather than emulating it, offering a measure of future-proofing against changes to the mouseover handler.
I think what you need is
$('.star.star_5').addClass('active');
Note the no-space between .star
and .star_5
and the _
in star_5
. (Thanks @wirey)
In CSS (which is what jQuery selectors are based on), .class1 .class2
means "an element with class2 that has an ancestor with class1". This is not what you want. You want .class1.class2
which means "an element with both class1 and class2":
$('.star.star_5').addClass('active');
In case you really want to simulate an event: You can trigger JavaScript events using parameterless forms of the corresponding jQuery functions. For example:
$('.star.star_5').mouseover();
本文标签: javascriptHow can I simulate a mouseover event with jqueryStack Overflow
版权声明:本文标题:javascript - How can I simulate a mouseover event with jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741497387a2381894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论