将事件处理程序添加到不存在的类?

时间:2015-04-08 20:17:59

标签: javascript jquery jquery-ui

我见过与不存在的元素有关的问题,但不存在与不存在的类有关的问题。这就是我想要做的。当一个按钮类#34; see_answer"单击,我想删除该类并将其替换为" see_question"。但是,我的按钮的单击功能,一旦它的类是" see_question",就没有运行。我尝试了$(document).on("click", ".see_question", function(event ),我尝试了$(".see_question").on("click", function(event) {etc...。谢谢您的帮助!我的代码如下:

$(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");

});

})

2 个答案:

答案 0 :(得分:1)

.see_question元素的点击处理委托给document。对于.see_answer元素,直接附加单击处理程序。因此,交换类名将产生不良影响。

  • see_answer生效时,点击将触发“see_answer”处理程序。
  • see_question生效时,点击将触发“see_question”处理程序和“see_answer”处理程序,仍然附加

有很多方法可以做到这一点。从您目前所处,最简单的解决方案是将.see_question .see_answer元素的点击处理委托给document

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

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

答案 1 :(得分:1)

结合2个处理程序,并在更改类周围之前找出hasClass()的版本

$(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 */
    }

});