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简单绑定单个事件的方法示例
Jun 10 jQuery
jQuery实现 RadioButton做必选校验功能
Jun 15 jQuery
jQuery动画_动力节点节点Java学院整理
Jul 04 jQuery
jquery.uploadView 实现图片预览上传功能
Aug 10 jQuery
jquery 获取索引值在一定范围的列表方法
Jan 25 jQuery
jQuery简单判断值是否存在于数组中的方法示例
Apr 17 jQuery
jQuery层叠选择器用法实例分析
Jun 28 jQuery
jquery实现垂直无限轮播的方法分析
Jul 16 jQuery
jQuery操作动画完整实例分析
Jan 10 jQuery
jQuery实时统计输入框字数及限制
Jun 24 jQuery
jQuery 实现DOM元素拖拽交换位置的实例代码
Jul 14 jQuery
jQuery实现推拉门效果
Oct 19 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
PHP5 安装方法
2006/10/09 PHP
PHP字符串处理的10个简单方法
2010/06/30 PHP
php 变量未定义等错误的解决方法
2011/01/12 PHP
php curl模拟post请求小实例
2013/11/13 PHP
php利用curl抓取新浪微博内容示例
2014/04/27 PHP
PHP内置的Math函数效率测试
2014/12/01 PHP
PHP将HTML转换成文本的实现代码
2015/01/21 PHP
详解PHP神奇又有用的Trait
2019/03/25 PHP
Yii框架视图、视图布局、视图数据块操作示例
2019/10/14 PHP
分享14个很酷的jQuery导航菜单插件
2011/04/25 Javascript
使用JavaScript 实现对象 匀速/变速运动的方法
2013/05/08 Javascript
js验证真实姓名与身份证号是否匹配
2015/10/13 Javascript
JS触摸屏网页版仿app弹窗型滚动列表选择器/日期选择器
2016/10/30 Javascript
JS和jQuery通过this获取html标签中的属性值(实例代码)
2017/09/11 jQuery
echarts饼图扇区添加点击事件的实例
2017/10/16 Javascript
小程序文字跑马灯效果
2018/12/28 Javascript
js中数组对象去重的两种方法
2019/01/18 Javascript
vue-router传参用法详解
2019/01/19 Javascript
Vue项目中使用mock.js的完整步骤
2021/01/12 Vue.js
[02:19]2014DOTA2国际邀请赛 专访820少年们一起去追梦吧
2014/07/14 DOTA
python自动zip压缩目录的方法
2015/06/28 Python
使用TensorFlow实现简单线性回归模型
2019/07/19 Python
FFrpc python客户端lib使用解析
2019/08/24 Python
Python如何使用turtle库绘制图形
2020/02/26 Python
Python Pandas 对列/行进行选择,增加,删除操作
2020/05/17 Python
python图片验证码识别最新模块muggle_ocr的示例代码
2020/07/03 Python
使用Python项目生成所有依赖包的清单方式
2020/07/13 Python
html5各种页面切换效果和模态对话框用法总结
2014/12/15 HTML / CSS
Html5饼图绘制实现统计图的方法
2020/08/05 HTML / CSS
Clarks英国官方网站:全球领军鞋履品牌
2016/11/26 全球购物
Gerry Weber德国官网:优质女性时装,德国最大的时装公司之一
2019/11/02 全球购物
三峡导游词
2015/01/31 职场文书
社区重阳节活动总结
2015/03/24 职场文书
项目经理助理岗位职责
2015/04/13 职场文书
阿凡达观后感
2015/06/10 职场文书
MYSQL如何查看操作日志详解
2022/05/30 MySQL