我正在尝试测试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已经在那里删除它.但由于某种原因,我失败了.
我知道get document.getElementById和IE 存在一些问题,但不确定为什么IE出现问题.remove而所有其他浏览器都没有.任何帮助在这里将不胜感激.我收到了错误
SCRIPT438: Object doesn't support property or method 'remove'
Run Code Online (Sandbox Code Playgroud)
从错误控制台.Javascript适用于所有其他浏览器.
这是代码:
<script type="text/javascript"><!--
function removeModule() {
<?php $tab = 1; ?>
var module_row = <?php echo $module_row; ?>;
if(!confirm('Are you sure?'))
{
return false;
}
var x = 1;
while (x < module_row)
{
if (document.getElementById('tab-' + x).checked)
{
document.getElementById('tab-' + x).remove();
document.getElementById('module-' + x).remove();
document.getElementById('tab-module-' + x).remove();
}
x++;
<?php $tab++; ?>
}
$('#form').submit();
}
//--></script>
Run Code Online (Sandbox Code Playgroud)
这是来自opencart模块,它是tpl文件.我在这里也包含了部分文件,所以希望有人可以发现错误.
<?php echo $header; ?>
<div id="content">
<div …Run Code Online (Sandbox Code Playgroud) 我有一个有趣的问题,我认为我已经掌握了它,但我想确定.我有一个调用名为remove()的函数的链接.除Chrome以外的所有浏览器都没有该功能的问题.但是,点击的链接在Chrome中消失了,即使我简化了下面示例中的功能.我见过这个问题:不能在javascript中使用"下载"作为函数名.但是,在链接中,我没有看到任何关于"删除"作为保留关键字的内容.我的问题是,我是关于这个关键字的正确吗?如果是这样,我可以在任何地方找到Google关键字列表吗?我搜索过,并没有发现这是其他地方的问题.
<a href="javascript:void(0)" onclick="remove()">Remove</a>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
function remove(){
alert("Hi");
}
Run Code Online (Sandbox Code Playgroud) 有没有一种使用 JavaScript“打开 div”的方法(删除父节点但保持其子节点完整)?
我的意思是改变这一点:
<div class="parent">
<div class="child">
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
</div>
<div class="child">
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
</div>
<div class="child">
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
</div>
<div class="child">
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
对此:
<div class="parent">
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
<div class="grandchild"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法访问 HTML,所以我想我可以在加载网页后使用 JavaScript 来实现此结果。
到目前为止我有:
const children = document.querySelectorAll('.child'); …Run Code Online (Sandbox Code Playgroud) 可能重复:
JavaScript:按ID删除元素
我只知道他们各自的id并且不知道他们的父节点....
如何使用Javascript删除具有特定标记名称的所有元素.例如,我做了以下事情:
var els = document.getElementsByTagName("center");
Run Code Online (Sandbox Code Playgroud)
它返回了所有center元素的数组.我该如何删除所有这些元素?
从即将删除JavaScript中的DOM节点的所有子元素和JavaScript的DOM元素删除我知道我可以遍历els,找到每个元素的父然后删除特定节点.但有没有其他方式由JavaScript提供.就像我们$('center').remove()在jquery中可以做的那样,它会删除带有center标记的所有元素.有什么类似于Javascript的东西?
我想使用Coffeescript 删除div具有特定class属性的元素.我在互联网上找不到任何关于使用Coffeescript进行DOM操作的例子.我怎样才能做到这一点?任何对DOM的引用都会很棒.
这是我的HTML
<div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的剧本
var list=document.getElementById("div2");
list.removeChild("div2""));
Run Code Online (Sandbox Code Playgroud)
当我点击按钮我需要删除子div(div2)如何做到这一点.使用此代码我面临问题请告诉.帮我 .我们有任何其他解决方案来解决这个问题
我有一个列表,每个bock的构造如下.有些街区有一个<span class="protected-icon"></span>.
我想制作一个非常简单的greasemonkey插件来删除该块.
所以,我的问题是使用Javascript如何删除/隐藏the entire block(<div data-item-type="user" class="js-stream-item stream-item"></div>包含它?
<div data-item-type="user" class="js-stream-item stream-item">
<div class="user-content-rest">
<span class="user-name">
<span class="protected-icon"></span>
</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我这样做了,似乎没有用!:
使用Javascript:
<script>
function hideOptionPhoto(){
var element = document.getElementById("Photo1");
element.parentNode.removeChild(Photo);
};
window.onload = function() {
hideOptionPhoto();
};
</script>
Run Code Online (Sandbox Code Playgroud)
HTML:
<div id=Photo1>
<input id="Photo" type="image" src="x.png" border="0" name="submit" alt="">
</div>
Run Code Online (Sandbox Code Playgroud)
因为父母和孩子的情况,我把<input>里面放了<div>.那是对的吗?
我正在尝试动态添加/删除DOM元素(id ="result").添加似乎工作正常,但删除后元素仍然出现在页面上.
这里发生了什么事?我究竟做错了什么?
这是我的代码:
<!DOCTYPE html>
<html>
<body>
<script>
function clearResult() {
if (document.getElementById("result") != null){
alert("remove #result");
$("#result").remove();
if (document.getElementById("result") != null) {
alert("#result still exists");
}
}
alert("Exiting clearResult");
}
function search() {
clearResult();
if (document.getElementById("result") == null) {
alert("add #result");
$('<table id="result"><tr>I am a table</tr></table>').appendTo('#ex');
}
}
</script>
<div>
<button id="search" onclick="search()" value="Send">Search</button>
</div>
<div id="ex">
@*Add result table here dynamically*@
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 此代码在Google Chrome中运行良好,但无法在Internet Explorer中使用:
document.getElementsByClassName('info')[i].remove();
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以做同样的事情,还是可以.remove()在Internet Explorer中工作?