我的带有 addEventHandler/click 的 JS 代码不起作用?

发布于 2024-12-18 09:27:13 字数 1167 浏览 0 评论 0原文

<script>
    var counter = 0;
    //Loads all the elements and assigns event handlers
    function load()
    {
        //create and append prev button
        
        loadPeople(counter);
        
        var btnNext = document.createElement("div");
        btnNext.setAttribute("class", "button");
        btnNext.setAttribute("id", "n");
        btnNext.innerHTML = "Next"
        root.appendChild(btnNext);
        
    }
    
    function loadPeople(index)
    {
        //This creates and appends several boxes with photos to represent various people
    }
    
    function addListeners()
    {
        //Add listeners
        //Previous
         
        //Next
        var eventNextClick = document.getElementById("n");
        eventNextClick.addEventListener("onclick", next, true);
    }
    
    //This should fire when next button is clicked
    function next()
    {
        alert("This works");
    }

    document.addEventListener("DOMContentLoaded", load, false)
    addListeners();
    
</script>

基本上......我的代码生成两个按钮上一个和下一个,以及几个显示图像的框。忽略这些框,现在,我只是想让点击事件起作用。

这段代码似乎没有产生任何可点击的内容。不确定我的问题出在哪里。请解释你的答案。

<script>
    var counter = 0;
    //Loads all the elements and assigns event handlers
    function load()
    {
        //create and append prev button
        
        loadPeople(counter);
        
        var btnNext = document.createElement("div");
        btnNext.setAttribute("class", "button");
        btnNext.setAttribute("id", "n");
        btnNext.innerHTML = "Next"
        root.appendChild(btnNext);
        
    }
    
    function loadPeople(index)
    {
        //This creates and appends several boxes with photos to represent various people
    }
    
    function addListeners()
    {
        //Add listeners
        //Previous
         
        //Next
        var eventNextClick = document.getElementById("n");
        eventNextClick.addEventListener("onclick", next, true);
    }
    
    //This should fire when next button is clicked
    function next()
    {
        alert("This works");
    }

    document.addEventListener("DOMContentLoaded", load, false)
    addListeners();
    
</script>

Basically...my code generates two buttons prev and next, and several boxes that show images. Disregard the boxes, right now, I am just trying to get the click event to work.

This code does not seem to produce anything clickable. Not sure where my problem is. Please explain your answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2024-12-25 09:27:13
eventNextClick.addEventListener("onclick", next, true);

onclick 应该只是 click

但请记住,在< IE9,使用 attachEvent() 时使用 on 前缀。

eventNextClick.addEventListener("onclick", next, true);

The onclick should just be click.

Remember though, in < IE9, when using attachEvent() to use the on prefix.

影子的影子 2024-12-25 09:27:13

eventNextClick.addEventListener("onclick", next, true);

您需要使用 "click" 而不是 "onclick"

您可能还想将第三个参数设置为 false,因为默认情况下您要使用冒泡。

document.addEventListener("DOMContentLoaded", load, false)
addListeners();

您还可以在绑定 DOM 就绪事件后直接添加侦听器。这行不通。您需要等到 load 触发并生成按钮,然后才能调用 addListeners

function load()
{
    // snip

    // invoke add listeners here instead
    addListeners();
}

eventNextClick.addEventListener("onclick", next, true);

You need to use "click" not "onclick"

You probably also want to set the third parameter to false since by default you want to use bubbling.

document.addEventListener("DOMContentLoaded", load, false)
addListeners();

Your also adding listeners directly after binding the DOM ready event. This wont work. You need to wait until load has fired and until the button has been generated before you invoke addListeners

function load()
{
    // snip

    // invoke add listeners here instead
    addListeners();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文