DOM 操作之获取值

  

  获得内容 - text():设置或返回所选元素的文本内容

    

 $("#btn1").click(function(){
alert("Text: " + $("#test").text());
});

  html() : 设置或返回所选元素的内容(包括 HTML 标记)

 $("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});

   val(): 设置或返回表单字段的值

 $("button").click(function(){
alert("Value: " + $("#test").val());
});

  获取属性 - attr() :

  

 <!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert($("#w3s").attr("href"));
});
});
</script>
</head> <body>
<p><a href="http://www.w3school.com.cn" id="w3s">W3School.com.cn</a></p>
<button>显示 href 值</button>
</body> </html>

  设置内容 - text()、html() 以及 val()

  • text() - 设置或返回所选元素的文本内容
  • html() - 设置或返回所选元素的内容(包括 HTML 标记)
  • val() - 设置或返回表单字段的值
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
});
</script>
</head> <body>
<p id="test1">这是段落。</p>
<p id="test2">这是另一个段落。</p>
<p>Input field: <input type="text" id="test3" value="Mickey Mouse"></p>
<button id="btn1">设置文本</button>
<button id="btn2">设置 HTML</button>
<button id="btn3">设置值</button>
</body>
</html>

  

 设置属性 - attr()

  jQuery attr() 方法也用于设置/改变属性值。attr() 方法也允许您同时设置多个属性。

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#ws").attr({
"href" : "http://www.ibbidream.cn/",
"title" : "jQuery 教程"
});
});
});
</script>
</head> <body>
<p><a href="http://www.baidu.com" id="ws">Baidu</a></p>
<button>改变 href 和 title 值</button>
<p>请把鼠标指针移动到链接上,或者点击该链接,来查看已经改变的 href 值和已经设置的 title 值。</p>
</body>
</html>

添加新的 HTML 内容

  我们将学习用于添加新内容的四个 jQuery 方法:

  • append() - 在被选元素的结尾插入内容
  •  <!DOCTYPE html>
    <html>
    <head>
    <script src="/jquery/jquery-1.11.1.min.js">
    </script>
    <script>
    $(document).ready(function(){
    $("#btn1").click(function(){
    $("p").append(" <b>Appended text</b>.");
    }); $("#btn2").click(function(){
    $("ol").append("<li>Appended item</li>");
    });
    });
    </script>
    </head> <body>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <ol>
    <li>List item </li>
    <li>List item </li>
    <li>List item </li>
    </ol>
    <button id="btn1">追加文本</button>
    <button id="btn2">追加列表项</button>
    </body>
    </html>
  • prepend() - 在被选元素的开头插入内容
  •  <!DOCTYPE html>
    <html>
    <head>
    <script src="/jquery/jquery-1.11.1.min.js"></script>
    <script>
    $(document).ready(function(){
    $("#btn1").click(function(){
    $("p").prepend("<b>Prepended text</b>. ");
    });
    $("#btn2").click(function(){
    $("ol").prepend("<li>Prepended item</li>");
    });
    });
    </script>
    </head>
    <body> <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <ol>
    <li>List item </li>
    <li>List item </li>
    <li>List item </li>
    </ol> <button id="btn1">添加文本</button>
    <button id="btn2">添加列表项</button> </body>
    </html>
  • after() - 在被选元素之后插入内容
  • before() - 在被选元素之前插入内容
  •  <!DOCTYPE html>
    <html>
    <head>
    <script src="/jquery/jquery-1.11.1.min.js"></script>
    <script>
    $(document).ready(function(){
    $("#btn1").click(function(){
    $("img").before("<b>Before</b>");
    }); $("#btn2").click(function(){
    $("img").after("<i>After</i>");
    });
    });
    </script>
    </head> <body>
    <img src="http://www.ibbidream.cn/img/ico.jpg" alt="test" />
    <br><br>
    <button id="btn1">在图片前面添加文本</button>
    <button id="btn2">在图片后面添加文本</button>
    </body>
    </html>

 删除元素/内容

  如需删除元素和内容,一般可使用以下两个 jQuery 方法:

  • remove() - 删除被选元素(及其子元素)
  •  <!DOCTYPE html>
    <html>
    <head>
    <script src="/jquery/jquery-1.11.1.min.js"></script>
    <script>
    $(document).ready(function(){
    $("button").click(function(){
    $("#div1").remove();
    });
    });
    </script>
    </head> <body> <div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">
    This is some text in the div.
    <p>This is a paragraph in the div.</p>
    <p>This is another paragraph in the div.</p>
    </div> <br>
    <button>删除 div 元素</button> </body>
    </html>
  • empty() - 从被选元素中删除子元素
  •  <!DOCTYPE html>
    <html>
    <head>
    <script src="/jquery/jquery-1.11.1.min.js"></script>
    <script>
    $(document).ready(function(){
    $("button").click(function(){
    $("#div1").empty();
    });
    });
    </script>
    </head> <body> <div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">
    This is some text in the div.
    <p>This is a paragraph in the div.</p>
    <p>This is another paragraph in the div.</p>
    </div> <br>
    <button>清空 div 元素</button> </body>
    </html>

JQuery dom 操作总结的更多相关文章

  1. jQuery DOM操作之结点转移复制

    jQuery DOM操作之结点转移复制 $('div').append($('p'))这样即可把p标签移动到div标签里 $('div').append( $('p').html() )是把p标签里的 ...

  2. JQuery DOM操作(属性操作/样式操作/文档过滤)

    jQuery——入门(三)JQuery DOM操作(属性操作/样式操作/文档过滤) 一.DOM属性操作 1.属性 (1).attr() 方法 语法:$(selector).attr(name|prop ...

  3. Jquery DOM 操作列表

    jQuery 文档操作方法 这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html(). jQuery 属性操作方法 下面列出的这些方法获得或设置元素的 DOM 属性. 这些方法对于 ...

  4. JQuery DOM操作 、属性和CSS样式操作、其他函数

    DOM操作 1.在div1内部最后追加一个节点 $("#div1").append("<img src='../01-HTML基本标签/img/Male.gif'/ ...

  5. 事件冒泡及事件委托的理解(JQuery Dom操作)

    jQuery事件冒泡: click mouseenter 等事件没有绑定 也会触发,只是触发后没有任何结果 子元素触发事件后,会把触发事件传递给父元素,那么父元素也会被触发. 不管有没有绑定事件,都会 ...

  6. jquery DOM操作(一)

    上一篇文章是记录了jquery中选择器的作用,这里只要记录下jquery中的DOM操作,还是按照上篇的格式来. 下面是测试用的html代码,接下来DOM的操作会在下面的代码下进行. <body& ...

  7. JQuery -- Dom操作, 示例代码

    1.内部插入节点 *   append(content) :向每个匹配的元素的内部的结尾处追加内容 *   appendTo(content) :将每个匹配的元素追加到指定的元素中的内部结尾处 *   ...

  8. Query节点操作,jQuery插入节点,jQuery删除节点,jQuery Dom操作

    一.创建节点 var box = $('<div>节点</div>'); //创建一个节点,或者var box = "<div>节点</div> ...

  9. jQuery DOM操作

    对节点的操作 查找节点 查找节点可以直接利用jQuery选择器来完成,非常便利. 插入节点 jQuery提供了8种插入节点的方法. 序号 方法 描述 实例 1 append() 向每个匹配的元素内部追 ...

随机推荐

  1. poi读取word2003(.doc文档)中的表格

    poi读取word2003(.doc文档)中的表格 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API.在网上见到好多通过po ...

  2. 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”

    解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合” 最近更新: 2013-2-15    587   很少写WinForm程序第一次使用ListBox控件就遇到了比 ...

  3. [NOIP 2014] 寻找道路

    [题目链接] http://uoj.ac/problem/19 [算法] 首先,在反向图上从终点广搜,求出每个点是否可以在答案路径中 然后在正向图中求出源点至终点的最短路,同样可以使用广搜 时间复杂度 ...

  4. Django 安装步骤

    Django的安装和简单使用 -安装: pip3 install django==1.11.9 pycharm 下安装,选择版本号, -使用: 命令创建项目:django-admin startpro ...

  5. ACM_来自不给标题的菜鸟出题组(巴什博弈+素数判定)

    来自不给标题的菜鸟出题组 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大B和小b合作出一道程序设计月赛的题,他们的想法是给定一 ...

  6. Ubuntu下搭建repo服务器(一): 配置gitosis

    1. 说明 服务器端IP: 192.168.1.126,下文简称:A端: 客户端IP: 192.168.130.19,下文简称:B端: Android工程代号:17435. 2. 安装必要软件(A端) ...

  7. Oracle 中文排序

        按照拼音顺序(常用)     ORDER BY nlssort(NAME, 'NLS_SORT=SCHINESE_PINYIN_M') 按照部首顺序 ORDER BY nlssort(NAME ...

  8. 几段Python小程序

    程序片段1 第一个需求是需要生成一些随机的时间,例如需要随机生成从一年前到现在的一些时间,刚开始折腾了半天,最后的代码如下: from datetime import timedelta from d ...

  9. 【oracle开发】wmsys.wm_concat介绍

    wmsys.wm_concat是一个聚合函数,其作用是将一列数据转换成一行,也就是我们常用的行专列,但是该函数是一个undocument函数,所以不推荐大家使用这个函数.因为在后续的版本中还提不提供这 ...

  10. Assembly之Instruction之Byte and Word

    Byte and word issues The MSP430 is byte-addressed, and little-endian. Word operands must be located ...