JQuery常见节点操作实例分析


Posted in jQuery onMay 15, 2019

本文实例讲述了JQuery常见节点操作。分享给大家供大家参考,具体如下:

插入节点

append()appengTo():在现存元素内部,从后面插入
prepend()prependTo():在现存元素外部,从前面插入
after()insertAfter():在现存元素外部,从后面插入
before()insertBefore():在现存元素外部,从前面插入

新建节点的插入

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
    $(function () {
      // $('#div1').html('')这种用字符串的方式塞进去的性能是最高的,
      // 但是有点时候不方便用,因为这样会重写div1里面的元素
      $a=$('<a href="#" rel="external nofollow" >链接</a>>');
      $('#div1').append($a);/*在最后加入字符串,append从现成的元素的后面插入元素*/
      $a.appendTo($('#div1'));/*和append效果相同*/
      $p=$('<p>这是一个p标签</p>');
      $("#div1").prepend($p);
      $h2=$('<h2>这是一个h2</h2>');
      $('#div1').after($h2);
      $h3=$('<h3>这是一个h3</h3>');
      $('#div1').before($h3);
    })
  </script>
</head>
<body>
  <div id="div1">
    <h1> 这是一个h1元素</h1>
  </div>
</body>
</html>

已有节点的插入

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
    $(function () {
      $('#p1').insertBefore($("#title01"));/*换两个节点顺序*/
    })
  </script>
</head>
<body>
  <h1 id="title01">这是一个h1元素</h1>
  <p id="p1">这是一个p元素</p>
  <span id="span01">这是一个span元素</span>
</body>
</html>

删除节点

remove():删除节点

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
    $(function () {
      $('#p1').insertBefore($("#title01"));/*换两个节点顺序*/
      $('#p1').remove();
    })
  </script>
</head>
<body>
  <h1 id="title01">这是一个h1元素</h1>
  <p id="p1">这是一个p元素</p>
  <span id="span01">这是一个span元素</span>
</body>
</html>

关于a标签的问题

<a href="javascript:alert('ok?');" rel="external nofollow" >链接</a>

如果这样写就是插入JavaScript语句,弹出ok,如果是写#就是连接到页面顶部。

todolist网页

实现一个用户自己列计划的网页

JQuery常见节点操作实例分析

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>todolist</title>
  <style type="text/css">
    .list_con {
      width: 400px;
      margin: 50px auto 0;
    }
    .inputtxt {
      width: 350px;
      height: 30px;
      border: 1px solid #ccc;
      padding: 0px;
      text-indent: 10px;
    }
    .inputbtn {
      width: 40px;
      height: 32px;
      padding: 0px;
      border: 1px solid #ccc;
    }
    .list {
      margin: 0;
      padding: 0;
      list-style: none;
      margin-top: 20px;
    }
    .list li {
      height: 30px;
      line-height: 30px;
      border-bottom: 1px solid #ccc;
    }
    .list li span {
      float: left;
    }
    .list li a {
      float: right;
      text-decoration: none;
      margin: 0 10px;
    }
  </style>
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script type="text/javascript">
    $(function () {
      var $inputtxt = $('#txt1');
      var $btn = $('#btn1');
      var $ul = $('#list');
      $btn.click(function () {
        var $val = $inputtxt.val();
        /*获取input框的值*/
        if ($val == "") {
          alert("请输入内容");
          return;
        }
        else {
          alert(1);
          var $li=$('<li><span>'+$val+'</span><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="up"> ↑ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="down"> ↓ </a><a\n' +
            '        href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">删除</a></li>');
          $li.appendTo($ul);
          $inputtxt.val("");/*获取到值之后,清空*/
          var $a=$li.find('.del');
          $a.click(function () {
            $(this).parent().remove();
          });
          $li.find('.up').click(function () {
            $(this).parent().insertBefore($(this).parent().prev());
          });
          $li.find('.down').click(function () {
            $(this).parent().insertAfter($(this).parent().next());
          });
        }
      });
      $del=$(".del");
      $del.click(function () {
        $(this).parent().remove();
      });
      $('.up').click(function () {
        $(this).parent().insertBefore($(this).parent().prev());
      });
      $('.down').click(function () {
        $(this).parent().insertAfter($(this).parent().next());
      });
    })
  </script>
</head>
<body>
<div class="list_con">
  <h2>To do list</h2>
  <input type="text" name="" id="txt1" class="inputtxt">
  <input type="button" name="" value="增加" id="btn1" class="inputbtn">
  <ul id="list" class="list">
    <li><span>学习html</span><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="up"> ↑ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="down"> ↓ </a><a
        href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">删除</a></li>
    <li><span>学习css</span><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="up"> ↑ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="down"> ↓ </a><a
        href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">删除</a></li>
    <li><span>学习javascript</span><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="up"> ↑ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="down">
      ↓ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">删除</a></li>
  </ul>
</div>
</body>
</html>

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.3water.com/code/HtmlJsRun 测试上述代码运行效果。

希望本文所述对大家jQuery程序设计有所帮助。

jQuery 相关文章推荐
jQuery鼠标悬停内容动画切换效果
Apr 27 jQuery
jQuery查找和过滤_动力节点节点Java学院整理
Jul 04 jQuery
浅谈jQuery框架Ajax常用选项
Jul 08 jQuery
jQuery绑定事件方法及区别(bind,click,on,live,one)
Aug 14 jQuery
jQuery实现倒计时功能 jQuery实现计时器功能
Sep 19 jQuery
Vue+jquery实现表格指定列的文字收缩的示例代码
Jan 09 jQuery
jQuery判断自定义属性data-val用法示例
Jan 07 jQuery
jQuery事件blur()方法的使用实例讲解
Mar 30 jQuery
jquery+php后台实现省市区联动功能示例
May 23 jQuery
jquery操作select常见方法大全【7种情况】
May 28 jQuery
jquery添加div实现消息聊天框
Feb 08 jQuery
jQuery--遍历操作实例小结【后代、同胞及过滤】
May 22 jQuery
JQuery属性操作与循环用法示例
May 15 #jQuery
jquery+css实现Tab栏切换的代码实例
May 14 #jQuery
jquery实现二级导航下拉菜单效果实例
May 14 #jQuery
JQuery获取可视区尺寸和文档尺寸及制作悬浮菜单示例
May 14 #jQuery
jquery 验证用户名是否重复代码实例
May 14 #jQuery
JQuery获取元素尺寸、位置及页面滚动事件应用示例
May 14 #jQuery
JQuery animate动画应用示例
May 14 #jQuery
You might like
在IIS7.0下面配置PHP 5.3.2运行环境的方法
2010/04/13 PHP
php 5.6版本中编写一个PHP扩展的简单示例
2015/01/20 PHP
学习php设计模式 php实现适配器模式
2015/12/07 PHP
window.showModalDialog使用手册
2007/01/11 Javascript
js 分页全选或反选标识实现代码
2011/08/09 Javascript
js实现右下角可关闭最小化div(可用于展示推荐内容)
2013/06/24 Javascript
jquery 取子节点及当前节点属性值的方法
2014/08/24 Javascript
微信小程序 定位到当前城市实现实例代码
2017/02/23 Javascript
jQuery EasyUI Panel面板组件使用详解
2017/02/28 Javascript
Mongoose经常返回e11000 error的原因分析
2017/03/29 Javascript
jQuery实现常见的隐藏与展示列表效果示例
2018/06/04 jQuery
React中使用UEditor百度富文本的方法
2018/08/22 Javascript
怎样在vue项目下添加ESLint的方法
2019/05/16 Javascript
JavaScript setInterval()与setTimeout()计时器
2019/12/27 Javascript
node爬取新型冠状病毒的疫情实时动态
2020/02/06 Javascript
NodeJS多种创建WebSocket监听的方式(三种)
2020/06/04 NodeJs
[07:20]2014DOTA2西雅图国际邀请赛 选手讲解积分赛第二天
2014/07/11 DOTA
[02:43]2018DOTA2亚洲邀请赛主赛事首日TOP5
2018/04/04 DOTA
教你如何在Django 1.6中正确使用 Signal
2014/06/22 Python
Python3使用PyQt5制作简单的画板/手写板实例
2017/10/19 Python
手把手教你python实现SVM算法
2017/12/27 Python
详解Python二维数组与三维数组切片的方法
2019/07/18 Python
Python partial函数原理及用法解析
2019/12/11 Python
html5版canvas自由拼图实例
2014/10/15 HTML / CSS
世界上最大的乐器零售商:Guitar Center
2017/11/07 全球购物
舒适的豪华鞋:Taryn Rose
2018/05/03 全球购物
CHARLES & KEITH英国官网:新加坡时尚品牌
2018/07/04 全球购物
瀑布模型都有哪些优缺点
2014/06/23 面试题
解决方案设计综合面试题
2015/08/31 面试题
介绍一下你对SOA的认识
2016/04/24 面试题
JAVA软件工程师测试题
2014/07/25 面试题
小学生校园广播稿
2014/09/28 职场文书
优秀教师申报材料
2014/12/16 职场文书
python对文档中元素删除,替换操作
2022/04/02 Python
Python序列化模块JSON与Pickle
2022/06/05 Python
Linux安装Docker详细教程
2022/07/07 Servers