zoukankan      html  css  js  c++  java
  • jQuery获取文本节点之 text()/val()/html() 方法区别

    1. 无参html():取得第一个匹配元素的html内容。这个函数不能用于XML文档。但可以用于XHTML文档,返回的是一个String
    例子:
    html页面代码:<div><p>Hello</p></div>
    jquery代码:$("div").html();
    结果:Hello
    2.有参html(val):设置每一个匹配元素的html内容。这个函数不能用于XML文档。但可以用于XHTML文档。返回一个jquery对象
    html页面代码:<div></div>
    jquery代码:$("div").html("<p>Nice to meet you</p>");
    结果:[ <div><p> Nice to meet you</p></div> ]

    其次,text属性中有两个方法,一个有参,一个无参
    1. 无参text():取得所有匹配元素的内容。结果是由所有匹配元素包含的文本内容组合起来的文本。返回的是一个String
    例子:
    html页面代码:<p><b>Hello</b> fine</p>
    <p>Thank you!</p>
    jquery代码:$("p").text();
    结果:HellofineThankyou!

    2.有参text(val):设置所有匹配元素的文本内容, 与 html() 类似, 但将编码 HTML (将 "<" 和 ">" 替换成相应的HTML实体).返回一个jquery对象
    html页面代码:<p>Test Paragraph.</p>
    jquery代码:$("p").text("<b>Some</b> new text.");
    结果:[ <p><b>Some</b> new text.</p> ]

    最后,val()属性中也有两个方法,一个有参,一个无参。
    1.无参val():获得第一个匹配元素的当前值。在 jQuery 1.2 中,可以返回任意元素的值了。包括select。如果多选,将返回一个数组,其包含所选的值。
    返回的是一个String、 array
    例子:
    html页面代码 : 

    <p></p><br/> 
    <select id="single"> 
    <option>Single</option> 
    <option>Single2</option> 
    </select> 
    <select id="multiple" multiple="multiple"> 
    <option selected="selected">Multiple</option> 
    <option>Multiple2</option> 
    <option selected="selected">Multiple3</option> 
    </select> 
    View Code

    jquery代码:$("p").append( "<b>Single:</b> " + $("#single").val() + " <b>Multiple:</b> " + $("#multiple").val().join(", "));
    结果:[ <p><b>Single:</b>Single<b>Multiple:</b>Multiple, Multiple3</p>]
    2.有参val(val):设置每一个匹配元素的值。在 jQuery 1.2, 这也可以为check,select,radio元件赋值,返回一个jquery对象
    html页面代码:
    <input type="text"/>
    jquery代码:$("input").val("hello world!");
    结果:hello world! 

    转自http://www.jb51.net/article/26431.htm

  • 相关阅读:
    2019.6.28 校内测试 T3 【音乐会】道路千万条
    2019.6.28 校内测试 T2 【音乐会】二重变革
    2019.6.28 校内测试 T1 Jelly的难题1
    CentOS7:ifconfig command not found解决和netstat -an
    centos系统查看本机IP地址
    centos 端口iptables配置
    centos -bash: netstat: command not found
    Centos 安装 NodeJS
    Go语言-变量和常量
    go get
  • 原文地址:https://www.cnblogs.com/ningheshutong/p/5505435.html
Copyright © 2011-2022 走看看