简体   繁体   中英

Adding event handler to non-existent class?

I've seen questions that relate to non-existent elements, but not non-existent classes. Here's what I want to do. When a button of class "see_answer" is clicked, I want to remove the class and replace it with "see_question". However, my click function for a button, once its class is "see_question", is not running. I have tried $(document).on("click", ".see_question", function(event ) and I have tried $(".see_question").on("click", function(event) {etc... . Thanks for the help! My code is below:

$(document).ready(function() {

// initialize variables
var lang = "javascript";
var qno = 1;
var prevText;       // holds question/answer
var language = lang + ".html";


// set up tabs, and keep track of which one is clicked
$("#myTabs").tabs({
    activate: function (event, ui) {
        var active = $("#myTabs").tabs("option", "active");
        lang = $("#myTabs ul > li a").eq(active).attr("href"); 
        lang = lang.replace("#", "");
    }
});

/* REMINDERS
    actual qa part: blah_language
*/

// set up question
$.ajax({
    url: language,
    dataType: "html",
    success: function(data) {
    $("#blah_"+lang)
    .text($(data).find("#1").text());
    },
    error: function(r) {
        alert("whoops, error in initialization");
    }
});


$(".next_question").on("click", function(event) {

    event.preventDefault();

    var id = $(this).attr("id").replace("next_question_", "");

    var language = id + ".html";
    var doc = "#blah_" + id;

    $.ajax({
        url: language,
        dataType: "html",
        success: function(data) {
            var num = "#" + qno;
            $(doc)
            .text($(data).find(num).text());
            qno = qno + 1;

        },
        error: function(r) {
            alert("whoops");
        }
    });
    prevText = "";
});



// SHOW ANSWER
$(".see_answer").on("click", function(event) {
    event.preventDefault();
    var id = $(this).attr("id").replace("see_answer_", "");
    var prev = "#blah_" + id;
    var answers = id + "_answers.html";


    // Save the question
    prevText = $(prev).text();
    var obj = $(this);

    $.ajax({
        url: answers,
        dataType: "html",
        success: function(data) {
            var num = "#" + 3;
            $(prev)
                .text($(data).find(num).text());
        },
        error: function(r) {
            alert("whoops");
        }
    });

    obj.val("See Question");
    obj.removeClass("see_answer");
    obj.addClass("see_question");
    event.stopPropagation();

});

$(document).on("click",".see_question",  function(event) {
    event.preventDefault();
        obj = $(this);
    event.preventDefault();
    var id = $(this).attr("id").replace("see_answer_", "");
    var prev = "#blah_" + id;
    $(prev).text(prevText);

    obj.val("See Answer");

    obj.removeClass("see_question");
    obj.addClass("see_answer");

});

})

Click handling for .see_question elements is delegated to document . For .see_answer elements, a click handler is attached directly. Therefore, swapping the class names will have an undesirable effect.

  • when see_answer is in force, a click will trigger the "see_answer" handler.
  • when see_question is in force, a click will trigger the "see_question" handler AND the "see_answer" handler, which is still attached .

There's a number of ways to do this properly. From where you currently are, the simplest solution is to delegate click handling of .see_question and .see_answer elements to document .

$(document).on("click", ".see_answer", function(event) {
    ...
});

$(document).on("click", ".see_question", function(event) {
    ...
});

Combine the 2 handlers and figure out which version it is by hasClass() before you change the classes around

$(document).on("click", ".see_question, .see-answer", function(event ){
    var $btn =$(this), isAnswer = $btn.hasClass('see_answer'); 

    // we know which one it is so can switch classes now
    $btn.toggleClass('see_answer see_question');

    if(isAnswer){
      /* run code for answer version */
    }else{
       /* run code for question version */
    }

});

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