I want to trigger a function when has an specific class, in this case Clicked
so I want to return the alert (unliked)
HTML
<div class="social">
<div class="LikePost default"></div>
</div>
JQUERY
$(".social").find(".LikePost").trigger("click"); /*when has class "clicked"*/
alert("unliked")
TRIGGERED FUNCTION
$('.LikePost').on('click', function() {
if ($(this).hasClass("default")) {
$(this).addClass("Clicked");
$(this).removeClass("default");
alert("post liked");
} else {
$(this).addClass("default");
$(this).removeClass("Clicked");
alert("post unliked");
}
});
好的xD
$(".Clicked").trigger("click")
You can rewrite your trigger like this:
$('.LikePost').on('click', function(){
// just invert classes state
$(this).toggleClass("default");
$(this).toggleClass("Clicked");
// if is default, its liked, if not, its not liked
if($(this).hasClass("default")) {
alert("post liked");
} else {
alert("post unliked");
}
});
And if you want to manually trigger all liked posts, just do it:
$('.Clicked').trigger('click');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.