Html 使用javascript通过ID删除输入元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18164352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:01:49  来源:igfitidea点击:

Remove input element by ID with javascript

javascripthtmlinput

提问by maxmitch

I have done this and it doesn't seem to be working!:

我已经这样做了,但它似乎不起作用!:

Javascript:

Javascript:

<script>
function hideOptionPhoto(){ 
    var element = document.getElementById("Photo1");
    element.parentNode.removeChild(Photo);   
};

window.onload = function() {
    hideOptionPhoto();
};
</script>

HTML:

HTML:

<div id=Photo1>
    <input id="Photo" type="image" src="x.png" border="0" name="submit" alt="">
</div>

I put the <input>inside a <div>because of the parent and child situation. Is that correct?

我把<input>里面的一个<div>因为父子的情况。那是对的吗?

回答by SarathSprakash

Try this out. This will work

试试这个。这将工作

The below script should be put inside the body tag

下面的脚本应该放在body 标签中

<script>
function hideOptionPhoto(){


var element = document.getElementById("Photo1");
var child=document.getElementById("Photo");
element.removeChild(child);

}
window.onload = function() {
  hideOptionPhoto();
};
</script>

回答by SarathSprakash

var element = document.getElementById("Photo"); // notice the change
element.parentNode.removeChild(element);

The <div>is optional (for this) since every element has a parentNode. But there might be other reasons for having the div.

<div>是可选的(为此),因为每个元素都有一个 parentNode。但可能还有其他原因导致 div。

回答by AnaMaria

Ok. Let me post the working fiddle and the I will give the explanation.

好的。让我发布工作小提琴,我将给出解释。

Working Fiddle

工作小提琴

In your code there was a "Syntax Error".

在您的代码中有一个"Syntax Error"

//Incorrect    
     <div id=Photo1>

//Correct
    <div id="Photo1">

In addition check my JavaScript function. The function call was ok.Just the code inside it was wrong

另外检查我的 JavaScript 函数。函数调用没问题。只是里面的代码错了

You already assigned the HTMLelement div(Photo1)to the variable "Element". The img("photo")is a childof Element and hence can be directly removed.

您已经将HTMLelement div(Photo1)分配给变量"Element"。所述IMG(“照片”)是一个元素,因此可以直接取出。

One more important point is the naming conventions that you use. You should not assign ID's like "photo"

更重要的一点是您使用的命名约定。您不应该分配像“照片”这样的 ID

HTML

HTML

<div id="Photo1">
    <input id="Photo" type="image" src="x.png" border="0" name="submit" alt=""/>
</div>

Javascript

Javascript

function hideOptionPhoto(){     
    var element = document.getElementById("Photo1");
    var child=document.getElementById("Photo");
    element.removeChild(child);
};

window.onload = function() {
    hideOptionPhoto();
};

回答by Waruna Manjula

Try

尝试

function hideOptionPhoto(){ 
var element =  document.getElementById('Photo');
if (typeof(element) != 'undefined' && element != null)
  {
    element.remove();
    alert('Deleted');
  }

};

window.onload = function() {
    hideOptionPhoto();
};
<div id=Photo1>
    <input id="Photo" type="image" src="x.png" border="0" name="submit" alt="">
</div>

回答by Sikander

<div id="Photo1">
<input id="Photo" type="image" src="x.png" border="0" name="submit" alt="">
</div>

Use this:

用这个:

document.getElementById("Photo1").innerHTML = "";