我正在尝试测试DOM元素是否存在,如果存在则删除它,如果不存在则创建它。
var duskdawnkey = localStorage["duskdawnkey"];
var iframe = document.createElement("iframe");
var whereto = document.getElementById("debug");
var frameid = document.getElementById("injected_frame");
iframe.setAttribute("id", "injected_frame");
iframe.setAttribute("src", 'http://google.com');
iframe.setAttribute("width", "100%");
iframe.setAttribute("height", "400");
if (frameid) // check and see if iframe is already on page
{ //yes? Remove iframe
iframe.removeChild(frameid.childNodes[0]);
} else // no? Inject iframe
{
whereto.appendChild(iframe);
// add the newly created element and it's content into the DOM
my_div = document.getElementById("debug");
document.body.insertBefore(iframe, my_div);
}
检查它是否存在,创建元素起作用,但删除元素不起作用。基本上,这段代码所做的就是通过点击按钮将一个iframe注入网页。我希望发生的是,如果iframe已经在那里删除它。但由于某种原因,我失败了。
https://stackoverflow.com/questions/8830839
复制相似问题