return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - event listener does not work - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - event listener does not work - Stack Overflow

programmeradmin3浏览0评论

Any ideas why this does not work.

The Yes button when clicked works once - the No button does not work

function $(x) {
    return document.getElementById(x);
}

var glob = 0;

function new_index() {
    glob += 1;
    return "d" + glob;
}

function play() {
    say("Hello is JS Fun?");
    response("No",
        function() {
            say("Oh dear")
        });
    response("Yes",
        function() {
            say("Great:");
        });
}

function say(x) {
    $("txt").innerHTML += x;
}

function response(Txt, Fun) {
    var n = new_index();
    var s = "<button id='" + n + "'>" + Txt + "</button>";
    say(s);
    var xx = $(n);
    // xx.onclick=Fun;
    xx.addEventListener("click", Fun);
}

play();
<div id="txt"></div>

Any ideas why this does not work.

The Yes button when clicked works once - the No button does not work

function $(x) {
    return document.getElementById(x);
}

var glob = 0;

function new_index() {
    glob += 1;
    return "d" + glob;
}

function play() {
    say("Hello is JS Fun?");
    response("No",
        function() {
            say("Oh dear")
        });
    response("Yes",
        function() {
            say("Great:");
        });
}

function say(x) {
    $("txt").innerHTML += x;
}

function response(Txt, Fun) {
    var n = new_index();
    var s = "<button id='" + n + "'>" + Txt + "</button>";
    say(s);
    var xx = $(n);
    // xx.onclick=Fun;
    xx.addEventListener("click", Fun);
}

play();
<div id="txt"></div>

Share Improve this question edited Sep 13, 2016 at 12:20 Adam Azad 11.3k5 gold badges31 silver badges72 bronze badges asked Sep 13, 2016 at 12:15 ja.ja. 1,3399 silver badges14 bronze badges 1
  • This question doesn't work. Can you please explain what do you expect your code to do, and what does it instead? – Teemu Commented Sep 13, 2016 at 12:17
Add a ment  | 

3 Answers 3

Reset to default 8

It's because every time you set the innerHTML it doesn't just add as you might think, it sets innerHTML to a new value and by that deleting the old elements with the old event listeners attached.

As others have mentioned, the main issue in your code is that you lose event bindings on subsequent innerHTML calls -- No stops working because it es after Yes. When Yes appends text it breaks itself.

When working with the DOM, it's better to create elements than alter HTML text (with innerHTML). In the case of your buttons it means you don't have to (1) create a button with an ID and then (2) find the button. With createElement you have a reference to it.

function $(x) {
    return document.getElementById(x);
}

var glob = 0;

function new_index() {
    glob += 1;
    return "d" + glob;
}

function play() {
    say(text("Hello is JS Fun?"));
    response("No",
        function() {
            say(text("Oh dear"));
        });
    response("Yes",
        function() {
            say(text("Great:"));
        });
}

function text(String) {
    var node = document.createElement("span");
    node.innerHTML = String;
    return node;
}

function say(x) {
    $("content").appendChild(x);
}

function response(Txt, Fun) {
    var button = document.createElement("button");
    button.innerHTML = Txt;
    button.addEventListener("click", Fun);
    say(button);
}

play();
<div id="content"></div>

The event handlers are lost on $("txt").innerHTML += x;.

发布评论

评论列表(0)

  1. 暂无评论