javascript - 如何使用可编辑的 HTML 页面和不同级别的用户添加元素来更新 javascript
问题描述
编辑:对于一般解决方案,这对我有用。动态创建元素的事件绑定?
所以我有一个看起来像这样的界面:
用户可以切换以显示或隐藏子列表。我使用 jquery 来选择切换按钮以使其工作:
$(".dropdownBtn").click(function(){
//rotate the button
$(this).parent().toggleClass("caret-down");
//hide the sublist
$(this).closest("li").find(".active").toggleClass("nested");
})
他们还可以为每个元素添加新的 sub_elements。例如,像这样:
$(".fa-plus-square").click(function(){
//if there's no new list, create a new unordered list
if ($(this).closest("li").find("ul").length == 0){
$(this).parents("li").append(newUL);
//and add a toggle button
$(this).parent().prepend(toggleBtn);
}
//add the element
$(this).closest("li").find("ul").append("<li>newElement</li>")
})
但是随后元素 3 下新添加的切换按钮不会响应。我相信我需要“绑定”新添加的按钮。但我不确定这样做的最佳做法是什么。
解决方案
推荐阅读
- c++ - 为什么向量中的第 -1 个索引始终为 0?
- javascript - 如果不适合输入标签,VueJs 在 textarea 中显示单词
- javascript - 如何在基于类的组件中使用“useDispatch”和“useSelector”
- jquery - 用于 jQuery 分页的 Kendo UI 不起作用 - “没有要显示的项目”
- python - 如何将 Pandas DataFrame 转换为 Newick 格式的字典
- haskell - Why this two function created by forkIO in haskell can't be run one by one?
- java - java.nio.file.FileSystemException: C:\test.csv -> C:\test2.csv: 该进程无法访问该文件,因为它正在被 java 中的另一个进程使用
- javascript - 间距和刻度颜色的胜利图表问题
- git - 如何在变基时将所有冲突集中在一起?
- typescript - 泛型 'T' 不可分配给类型为 'never' typescript 的参数