简体   繁体   English

删除一个 <p> 使用JavaScript

[英]remove a single <p> using javascript

In my code, if the <input> is empty on blur, then a <p> containing a warning message is added with JavaScript. 在我的代码中,如果<input>在模糊时为空,则包含警告消息的<p>将与JavaScript添加在一起。 I remove the <p> when the <input> is focused again. <input>再次聚焦时,我将<p>删除。

How can I do this without the for loop, since the <div> only has one <p> ? 由于<div>只有一个<p> ,我该如何不使用for循环?
I'd like to do this with pure JavaScript (no jQuery). 我想使用纯JavaScript(不使用jQuery)进行此操作。

 function produce_warning(message, div, inpt) { var para = document.createElement("p"); para.setAttribute("class", "warning_msg"); var node = document.createTextNode(message); para.appendChild(node); var element = document.getElementById(div); element.appendChild(para); } function remove_warning(div) { var element = document.getElementById(div).getElementsByTagName("p"); for (var k = element.length - 1; k >= 0; k--) { var item = element[k]; item.parentNode.removeChild(item); } } 
 <div class="col-sm-10 col-lg-5" id="name_div"> <input type="text" class="form-control" onblur="produce_warning('name', 'name_div')" onfocus="remove_warning('name_div')" id="name" placeholder="Full Name"> </div> 

Since the div element will ever only have one p element, then you just have to target the first index of the array variable for removal: 由于div元素永远只有一个p元素,因此您只需定位要删除的数组变量的第一个索引:

function remove_warning(){
    // This returns an array
    var element = document.getElementsByTagName("p");
    // Only one p element, so remove from the first index
    element[0].parentNode.removeChild(element[0]);
}

Lloyd Banks's solution should work and is probably the best way to do it if you want to support older browsers. 劳埃德银行(Lloyd Banks)的解决方案应该可以工作,并且如果要支持较旧的浏览器,可能是最好的解决方案。 Modern browsers also support document.querySelector and document.querySelectorAll. 现代浏览器还支持document.querySelector和document.querySelectorAll。 querySelector / querySelectorAll let you select elements using CSS selectors. querySelector / querySelectorAll使您可以使用CSS选择器选择元素。 querySelectorAll will select all matching elements, while querySelector just selects the first one. querySelectorAll将选择所有匹配的元素,而querySelector仅选择第一个。 So here's an alternative solution: 所以这是一个替代解决方案:

//pass the div id to this function,  for example "name_div"
function remove_warning(div){
    // This grabs the first p within the element with id div
    var element = document.querySelector("#"+div+" p");
    // go to element's parent and tell it to remove the element child
    // cause in javascript a child's gotta ask his parent
    element.parentNode.removeChild(element);
}

Here's a list of browser compatibility for querySelector: http://www.w3schools.com/jsref/met_document_queryselector.asp 以下是querySelector的浏览器兼容性列表: http : //www.w3schools.com/jsref/met_document_queryselector.asp

I don't think you can do with just JavaScript but if you add <script src="http://code.jquery.com/jquery-2.1.4.js"></script> in your page somewhere you have access to Jquery. 我认为您不能只使用JavaScript,但如果在您可以访问的页面中添加<script src="http://code.jquery.com/jquery-2.1.4.js"></script>到jQuery。 You would remove this <p id = "removeThis">some text</p> with this <script> $("#removeThis").remove(); </script> 您可以使用此<script> $("#removeThis").remove(); </script>删除此<p id = "removeThis">some text</p> <script> $("#removeThis").remove(); </script> <script> $("#removeThis").remove(); </script> . <script> $("#removeThis").remove(); </script>

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

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