JavaScript实现添加、查找、删除元素

 更新时间:2015年07月02日 11:15:45   投稿:hebedich  
这篇文章主要汇总介绍了JavaScript实现添加、查找、删除元素的方法,十分的简单实用,有需要的小伙伴可以参考下。

代码很简单,这里就不多废话了。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试文件</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: 楷体;
  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="">中央情报局</a>:中央情报局</div>
  <div class="time">2014年5月4日21:56</div>
  <div class="replyButton">
   <input type="button" id="submitButton" value="回复" onClick="showReplyArea(this)" />
  </div>
  <div class="replyButton">
   <input type="button" id="cancelButton" value="取消" onClick="hideReplyArea(this)" />
  </div>
 </div>
</div>
<script>
//显示文本框的函数
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 = '提交';
  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 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;
}

//下面为调试时测试用函数,做完后就没用了,但也是很经典的一段代码,就留在这里了。
function showNode()
{
  var i = 4;
  submitButton = document.getElementById('submitButton');
  i = submitButton.parentNode.parentNode.getElementsByTagName("INPUT").item(1).value;
  alert(i);
}
</script>
</body>
</html>

示例二:

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|^ ) 判断前面是否有空格 (\\s | $ )判断后面是否有空格 两个感叹号为转换为布尔值 以方便做判断 
}; 
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方法是替换 
}; 
}; 
};

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • JavaScript中window和document用法详解

    JavaScript中window和document用法详解

    这篇文章主要介绍了JavaScript中window和document用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • uniapp解决软键盘弹出问题方法详解

    uniapp解决软键盘弹出问题方法详解

    这篇文章主要为大家介绍了uniapp解决软键盘弹出问题方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 原生JS实现的碰撞检测功能示例

    原生JS实现的碰撞检测功能示例

    这篇文章主要介绍了原生JS实现的碰撞检测功能,涉及javascript鼠标事件响应及页面图形坐标位置运算、检测相关操作技巧,需要的朋友可以参考下
    2018-05-05
  • Js md5加密网页版MD5转换代码

    Js md5加密网页版MD5转换代码

    Js实现网页上的MD5加密功能,将文字转换为MD5字符,本代码调用简单,你可以新建一个网页,将此网页上传到你的服务器上,用户浏览网页,就可实现MD5加密转换功能,用户可方便查询任一字符的MD5码,很不错的功能
    2013-03-03
  • javascript实现C语言经典程序题

    javascript实现C语言经典程序题

    这篇文章主要介绍了javascript实现C语言经典程序题的解题思路,感兴趣的小伙伴们可以参考一下
    2015-11-11
  • JS中多步骤多分步的StepJump组件实例详解

    JS中多步骤多分步的StepJump组件实例详解

    这篇文章主要介绍了JS中多步骤多分步的StepJump组件实例详解的相关资料,需要的朋友可以参考下
    2016-04-04
  • JS实现不规则TAB选项卡效果代码

    JS实现不规则TAB选项卡效果代码

    这篇文章主要介绍了JS实现不规则TAB选项卡效果代码,通过简单的JavaScript响应鼠标事件动态变换元素样式实现不规则选项卡效果,非常具有实用价值,需要的朋友可以参考下
    2015-09-09
  • 使用Bootstrap和Vue实现用户信息的编辑删除功能

    使用Bootstrap和Vue实现用户信息的编辑删除功能

    这篇文章主要介绍了使用Bootstrap和Vue实现用户信息的编辑删除功能,需要的朋友可以参考下
    2017-10-10
  • JavaScript闭包的简单应用

    JavaScript闭包的简单应用

    这篇文章主要为大家详细介绍了JavaScript闭包的简单应用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • js实现模态窗口增加与删除

    js实现模态窗口增加与删除

    这篇文章主要为大家详细介绍了js实现模态窗口增加与删除,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07

最新评论