简体   繁体   中英

$(“id”).trigger(“click”) not calling :active selector associated with that element

So I have a button with id="one" and I have set the :active selector of that button

#one{
  background:#00cc00;
}
#one:active{
  background:#00ff00;
}

Now I am triggering the above button using some other button but it does not trigger the :active selector

 $("#startBtn").on("click", function() {

    $("#one").trigger("click");

  });

What do I have to do for triggering :active selector? I have already bind the click event for a class which contains the button

 $(".squares").on("click", function() {

    var id = $(this).attr("id");
    new Audio(audio[mainIndex[id]]).play();

  });

So the audio plays with the .trigger event but the :Active selector doe not tigger.

jQuery events do not trigger CSS pseudo-selectors (focus, hover etc).

If you want to change the color of the other button, you will need to either set the style, or add a class with the style you want:

#one{
  background:#00cc00;
}
#one.active{
  background:#00ff00;
}

$("#startBtn").on("click", function() {
    $("#one").addClass("active");
    $("#one").trigger("click");
});

You need to bind an Event before you trigger the same. Like this:

 $("#one").on("click", function() {
     alert("Button click triggered");
 });
 $("#startBtn").on("click", function() {
     $("#one").trigger("click");
 });

:active is an interaction state. I would try this using :focus

 $("#startBtn").on("click", function() { $("#one").trigger('focus'); }); 
 #one{ background:#00cc00; } #one:focus{ background:#00ff00; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <input type="button" id="startBtn" value="startBtn"> <input type="button" id="one" value="one"> 

Try this

 $("#one").on("click", function() { console.log("Clicked One"); }); $('#startBtn').mousedown(function() { $("#one").addClass('active').trigger("click"); }).mouseup(function() { $("#one").removeClass('active'); }); 
 #one { background: red; } #one:active { background: #00ff00; } .active { background: #00ff00 !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="startBtn">start</button> <button id="one">one</button> 

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.

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