The JavaScript implementation adds finds and removes elements

  • 2020-06-22 23:52:52
  • OfStack

The code is very simple, so let's not talk too much about it.


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> The test file </title>
<style>
.reply {
  width: 500px;
  height: 100%;
  overflow: hidden;
  background-color:#CCC;
  margin-top: 10px;
}
.infoArea {
  width: 380px;
  height: 100%;
  overflow: hidden;
}
.words {
  width: 380px;
  height: auto;
  margin: 5px 0px;
  float: left;
  font-size: 14px;
}
.time {
  margin-left: 10px;
  margin-bottom: 3px;
  width: 150px;
  height: 20px;
  line-height: 20px;
  float: left;
  font-family:  Regular script ;
  font-size: 14px;
  color: #999;
}
.replyButton {
  width: 60px;
  height: 20px;
  float: left;
  margin-bottom: 10px;
}
.replyButton input {
  font-size: 12px;
}
#cancelButton {
  visibility: hidden;
}
</style>
</head>

<body>
<div class="reply">
 <div class="infoArea">
  <div class="words"><a href=""> Central Intelligence Agency </a>: Central Intelligence Agency </div>
  <div class="time">2014 years 5 month 4 day 21:56</div>
  <div class="replyButton">
   <input type="button" id="submitButton" value=" reply " onClick="showReplyArea(this)" />
  </div>
  <div class="replyButton">
   <input type="button" id="cancelButton" value=" cancel " onClick="hideReplyArea(this)" />
  </div>
 </div>
</div>
<script>
// Function to display a text box 
function showReplyArea(src)
{
  inputText = document.createElement("DIV");
  inputText.className = "inputText";
  inputText.style.width = '100%';
  inputText.style.height = '75px';
  inputText.style.margin = '3px 0';
  inputText.style.cssFloat = 'left';
  
  var grandfather = src.parentNode.parentNode.parentNode;
  grandfather.appendChild(inputText);
  
  form1 = document.createElement("FORM");
  form1.action = "";
  form1.method = "post";
  inputText.appendChild(form1);
  
  inputTextArea = document.createElement("TEXTAREA");
  inputTextArea.style.width = '380px';
  inputTextArea.style.height = '40px';
  inputTextArea.style.marginLeft = '60px';
  inputTextArea.style.marginTop = '3px';
  inputTextArea.style.cssFloat = 'left';
  inputTextArea.style.resize = 'none';
  
  textSubmit = document.createElement("INPUT");
  textSubmit.type = 'submit';
  textSubmit.value = ' submit ';
  textSubmit.style.marginLeft = '80px';
  textSubmit.style.cssFloat = 'left';
  
  form1.appendChild(inputTextArea);
  form1.appendChild(textSubmit);
  
  cancelButton = grandfather.getElementsByTagName("INPUT").item(1);
  cancelButton.style.visibility = 'visible';
  submitButton = src;
  submitButton.disabled = true;
}
// Function to hide text boxes 
function hideReplyArea(src)
{
  var grandfather = src.parentNode.parentNode.parentNode;
  var inputText = grandfather.getElementsByClassName('inputText').item(0);
  grandfather.removeChild(inputText);
  
  cancelButton = src;
  cancelButton.style.visibility = 'hidden';
  submitButton = grandfather.getElementsByTagName("INPUT").item(0);
  submitButton.disabled = false;
}

// The following is a test function for debugging. It is useless after you finish it, but it is also very classic 1 I'm just going to leave it here. 
function showNode()
{
  var i = 4;
  submitButton = document.getElementById('submitButton');
  i = submitButton.parentNode.parentNode.getElementsByTagName("INPUT").item(1).value;
  alert(i);
}
</script>
</body>
</html>

Example 2:


window.onload = function(){ 
var gaga = document.getElementById( "gaga" ); 
addClass( gaga,"gaga1" ) 
addClass( gaga,"gaxx" ); 
removeClass( gaga,"gaga1" ) 
removeClass( gaga,"gaga" ) 
function hasClass( elements,cName ){ 
return !!elements.className.match( new RegExp( "(\\s|^)" + cName + "(\\s|$)") ); // ( \\s|^ )  Determine if there is any space in front of it   ( \\s | $  ) Determine if there is any space after it   Two exclamation marks are converted to Boolean values   To facilitate judgment  
}; 
function addClass( elements,cName ){ 
if( !hasClass( elements,cName ) ){ 
elements.className += " " + cName; 
}; 
}; 
function removeClass( elements,cName ){ 
if( hasClass( elements,cName ) ){ 
elements.className = elements.className.replace( new RegExp( "(\\s|^)" + cName + "(\\s|$)" )," " ); // replace The method is substitution  
}; 
}; 
};

This is the end of this article, I hope you enjoy it.


Related articles: