我正在尝试测试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);
}
Run Code Online (Sandbox Code Playgroud)
检查它是否存在有效,创建元素有效,但删除元素则不起作用.基本上所有这些代码都是通过单击按钮将iframe注入网页.我想要发生的是iframe已经在那里删除它.但由于某种原因,我失败了.
我正在使用以下代码,但在IE中失败了.消息是:
无法获取属性'add'的值:object为null或undefined"
我认为这只是一个IE支持问题.你如何使以下代码在IE中工作?
有任何想法吗?
var img = new Image();
img.src = '/image/file.png';
img.title = 'this is a title';
img.classList.add("profilePic");
var div = document.createElement("div");
div.classList.add("picWindow");
div.appendChild(img);
content.appendChild(div);
Run Code Online (Sandbox Code Playgroud) 我制作了一个模式来使用他们的原型创建和扩展html元素.这就像非浏览器中的魅力一样.示例代码可以在@jsbin找到(参见页面源代码)
这种模式的优点应该是速度(方法在元素原型链中,因此它们被引用一次).你猜对了:IE没有去.在IE <8中,html元素的原型是隐藏/不可访问的,因此对于您创建的每个元素,您必须再次引用非标准方法(如果您密集使用该模式,请留下很多指针).我在网上搜索了解决方案,但只找到了复杂的解决方法.有没有真的没有办法在IE浏览器访问HTML元素的原型?