Javascript jQuery函数。一个没有工作

Javascript jQuery函数。一个没有工作,javascript,jquery,Javascript,Jquery,每次单击add图标时,javascript文件中的addFunction()都会调用。每次函数调用时,它都会在无序列表中附加一个click函数 为了避免连接多个单击函数,我使用了.one而不是.on,但这种方法不起作用 HTML: Javascript: addFunction = function() { const txt = prompt(promptMsg1, promptMsg2); if (!(txt == null || txt == "")) {

每次单击add图标时,javascript文件中的addFunction()都会调用。每次函数调用时,它都会在无序列表中附加一个click函数

为了避免连接多个单击函数,我使用了.one而不是.on,但这种方法不起作用

HTML:

Javascript:

addFunction = function() {
    const txt = prompt(promptMsg1, promptMsg2);
    if (!(txt == null || txt == "")) {

        const i = document.createElement("i");
        i.classList.add("fa");
        i.classList.add("fa-times");
        i.classList.add('red-cross');

        const a = document.createElement("a");
        a.classList.add("ml-4");
        a.append(i);

        const t = document.createTextNode(txt)

        const newItem = document.createElement("li");
        newItem.append(t);
        newItem.append(a);

        var final = $("ul#classesAndSubjectsList");
        // No idea why .one is not working
        final.one('click', 'li a', function () {
            //other code
        });
        $(final).append(newItem);
     }
 }

 $("a#classesAndSubjects").click(function(){
      addFunction();
 });

一个地方做
final。一个地方做
,另一个地方做
$(final)。附加
,也许这会给你一个线索,试试
$(final)。一个地方做
你得到的错误是什么?无序列表中有多个单击函数被附加。我只需要一次点击功能。
addFunction = function() {
    const txt = prompt(promptMsg1, promptMsg2);
    if (!(txt == null || txt == "")) {

        const i = document.createElement("i");
        i.classList.add("fa");
        i.classList.add("fa-times");
        i.classList.add('red-cross');

        const a = document.createElement("a");
        a.classList.add("ml-4");
        a.append(i);

        const t = document.createTextNode(txt)

        const newItem = document.createElement("li");
        newItem.append(t);
        newItem.append(a);

        var final = $("ul#classesAndSubjectsList");
        // No idea why .one is not working
        final.one('click', 'li a', function () {
            //other code
        });
        $(final).append(newItem);
     }
 }

 $("a#classesAndSubjects").click(function(){
      addFunction();
 });