简体   繁体   English

如何使用javascript将html5主要元素定位为父节点,以删除主要元素子元素

[英]How to target html5 main element as a parent node using javascript to remove the main elements children

Hi as the title say I am wondering how I can target the tag to use as a parent node using javascript so I can remove the contents of it (all 's children). 您好,如标题所说,我想知道如何使用javascript来定位标记以用作父节点,以便删除其内容(所有子项)。 Lets start with my html (I want to remove the button) 让我们从我的html开始(我要删除按钮)

<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="style.css">

    <title></title>


</head>

<body>

    <main>
        <input type="button" id="start" name="startQ" value="Start Quiz">
    </main>
<script src="script.js"></script>    
</body>

</html>

now I am kinda lost on how to target that main tag. 现在我有点不知道如何定位主要标签。 I know if I change main --> div id=main I could use the js code: 我知道是否可以更改main-> div id = main我可以使用js代码:

var x = document.getElementById("main");
x.parentNode.removeChild(x);

But id like to use main Any ideas? 但是id喜欢使用main有什么想法吗?

Thanks in advanced! 提前致谢!

to remove main tag 删除主标签

var x = document.getElementsByTagName('main')[0];
x.parentNode.removeChild(x);

to remove button 删除按钮

var x = document.getElementById("start");
x.parentNode.removeChild(x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM