我正在为JS创建一个库,该库允许创建,删除和编辑窗口小部件。创建新的小部件时,它会自动获取一个ID,例如“ widget5”或数组中的下一个数字。删除后,document.getElementById()找不到ID,并返回null。
我意识到有关此问题有很多问题,答案通常与页面之前的JavaScript加载有关,但是由于我正在编写的窗口小部件库在页面加载后会添加窗口小部件,所以这不是问题。
我认为问题在于,当使用JS创建小部件时,JS由于某种原因找不到它。例如,如果我在小部件中进行了硬编码,则JS可以轻松找到ID。我怎样才能解决这个问题?
任何帮助深表感谢!
http://codepen.io/amstrudy/pen/grxpOK
//this is just the function that is supposed to find the id, check the codepen above for the full code
function deleteWidget(){
var widget = document.getElementById("delList");
widget = widget.options[widget.selectedIndex].text;
window.alert(widget);
widget = document.getElementById(widget);
window.alert(widget);
document.getElementById("body").removeChild(widget);
}
小部件ID内有一个空格:
div.setAttribute("id", " widget" + widgetNum);
^ here
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句