javascript试图删除某些标签的所有内容 [英] javascript trying to remove all things with certain tags
问题描述
我正在尝试使用javascript来删除页面上的按钮或输入标签的所有内容...到目前为止,我的代码删除了其中一些,我不知道为什么。它只删除了许多中的一个复选框和2个按钮(有3个按钮)
I'm trying to use javascript to remove everyhting with button or input tags from a page... So far my code removes some of them and I dont know why. It only removes one checkbox out of many, and 2 buttons (there are 3 buttons)
var buttons = document.getElementsByTagName("button");
for (var j = 0; j < buttons.length ; j++)
{
buttons[j].parentNode.removeChild(buttons[j]);
}
var checkboxes = document.getElementsByTagName("input");
for (var j = 0; j < buttons.length ; j++)
{
checkboxes[j].parentNode.removeChild(checkboxes[j]);
}
推荐答案
var checkboxes = document.getElementsByTagName("input");
for (var j = 0; j < buttons.length ; j++) // You use the wrong variable here
{
checkboxes[j].parentNode.removeChild(checkboxes[j]);
}
应该是;
for (var j = 0; j < checkboxes.length ; j++)
关于未删除的按钮,可能是因为它是输入类型=按钮
而不是< button>
?
Regarding to the undeleted button, maybe it's because it's an input type="button"
and not a <button>
?
请注意 document.getElementsByTagName(input);
将获取和删除以后所有输入,而不仅仅是复选框!
Note that document.getElementsByTagName("input");
will get and delete later all the inputs, not just the checkboxes!
我建议您使用像jQuery这样的javascript库吗?你可以这样做一行代码没有问题:
May I suggest you using a javascript library like jQuery? you could have done this will one line of code with no problems:
$('input[type="button"], input[type="checkbox"], button').remove();
这篇关于javascript试图删除某些标签的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!