简体   繁体   English

用于图像的 onclick、onmouseover、onmouseout 的 JavaScript 版本

[英]JavaScript version of onclick, onmouseover, onmouseout for an image

I'd like to be able to call functions for onmouseover, onmouseout, and onclick of an image, purely using javascript, not HTML.我希望能够调用图像的 onmouseover、onmouseout 和 onclick 函数,完全使用 javascript,而不是 HTML。 I have code that creates a dynamic list of paragraph tags on the page using purely javascript.我有代码可以使用纯 javascript 在页面上创建一个动态的段落标签列表。 It looks like a list but is not a list.它看起来像一个列表,但不是一个列表。 I've also added an x image to the left of each line of the list.我还在列表的每一行的左侧添加了一个 x 图像。 I'd like when that image is clicked, to call a function to remove that line.我希望在单击该图像时调用一个函数来删除该行。 Remember the image was added via JavaScript not HTML.请记住,图像是通过 JavaScript 而不是 HTML 添加的。 How do I do this?我该怎么做呢?

Here is my code:这是我的代码:

updateAllDataDisplay(false);
function updateAllDataDisplay(addOne) {

    // Updating the entire display
    if (!addOne) {
        for (var i = 0; i < allData.length; i++) {
            let a = document.createElement("p");
            let c = document.createElement("img");
            c.src = "delete_button_smallest_still.png";
            c.addEventListener('click', removeLine(`${allData[i].month}/${allData[i].day}/${allData[i].year} (${allData[i].hours}:${allData[i].mins}:${allData[i].secs}): ${allData[i].exerciseHours}. ${allData[i].food}. ${allData[i].weight}. ${allData[i].mood}.`), false);
            a.appendChild(c);
            let d = document.createTextNode(" ");
            a.appendChild(d);
            let b = document.createTextNode(`${allData[i].month}/${allData[i].day}/${allData[i].year} (${allData[i].hours}:${allData[i].mins}:${allData[i].secs}): ${allData[i].exerciseHours}. ${allData[i].food}. ${allData[i].weight}. ${allData[i].mood}.`);
            a.appendChild(b);    
            document.body.appendChild(a); 
        }

    // Updating the last item of the display
    } else if (addOne) {
        let a = document.createElement("p");
        if (allData.length > 0) {
        let b = document.createTextNode(`${allData[allData.length - 1].month}/${allData[allData.length - 1].day}/${allData[allData.length - 1].year} (${allData[allData.length - 1].hours}:${allData[allData.length - 1].mins}:${allData[allData.length - 1].secs}): ${allData[allData.length - 1].exerciseHours}. ${allData[allData.length - 1].food}. ${allData[allData.length - 1].weight}. ${allData[allData.length - 1].mood}.`);
        a.appendChild(b);
        document.body.appendChild(a);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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