从“加载时”页面处理未加载的对象

时间:2019-06-02 20:22:10

标签: javascript jquery

我需要jQuery帮助。 页面加载后,我有一个标签:

因此,我可以通过以下方式在脚本中选择它:$('#root-directory')

然后,我选择它并使用.html()jQuery函数在单击它时在其中添加一些内容。

这些内容包含了页面中从未使用过的带有类名称(例如:“ has-child”)的新元素。

在那之后,我创建了一个必须操纵类has-child的函数。

问题出现在此函数上,即使可以在DOM中检查元素,它也会找到此类的任何元素。 同样,如果我使用此类名称创建触发器,则它将返回错误。

因此,我正在寻找的解决方案是如何操纵脚本中的“具有孩子”元素。

<ul id="root-directory"></ul>
let root = $('#root-directory');

$(root).on('click', function(){
    let html="";
    for(let i = 0; i < 4; i++) {
        if(i % 2 === 0) {
            html += `<li>item number ${i}</li>`
        }
        else html += `<li clas="has-child">item number ${i} with child</li>`
    }
})

$('li.has-cild').click(function(){
    //...Add some new contents
})
//And this return a reference error because the selection is said undefined.

0 个答案:

没有答案