jquery - 设置/获取内容和属性

一般我们会遇到给某个元素添加或更改原有的文字:

1. 设置/获取内容 - text()、html() 以及 val()

  • 设置内容常用的三个方法:

    text() - 设置或返回所选元素的文本内容
    html() - 设置或返回所选元素的内容(包括 HTML 标记)
    val() - 设置或返回表单字段的值
    eg: 通过 text()、html() 以及 val() 方法来设置/获取内容:
    $("#btn1").click(function(){
      $("#test1").text("Hello world!");   //设置
      //获取:$("#test1").text();
    });
    $("#btn2").click(function(){
      $("#test2").html("<b>Hello world!</b>");   //设置
      //获取: $("#test2").html();
    });
    $("#btn3").click(function(){
      $("#test3").val("Dolly Duck");   //设置
      //获取:$("#test3").val();
    });
  • text()、html() 以及 val() 的回调函数
    
    上面的三个 jQuery 方法:text()、html() 以及 val(),同样拥有回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。
    eg: 带有回调函数的 text() 和 html():
    
    $("#btn1").click(function(){
      $("#test1").text(function(i,origText){
        return "Old text: " + origText + " New text: Hello world!
        (index: " + i + ")";
      });
    });
    
    $("#btn2").click(function(){
      $("#test2").html(function(i,origText){
        return "Old html: " + origText + " New html: Hello <b>world!</b>
        (index: " + i + ")";
      });
    });

2. 设置/获取属性 - attr()

  • jQuery attr() 方法也用于设置改变/获取属性值。

    eg: 改变(设置)链接中 href 属性的值:
    
    $("button").click(function(){
      $("#w3s").attr("href","http://www.w3school.com.cn/jquery");  //设置改变   //获取:$("#w3s").attr("href");
    });
  • attr() 方法也允许您同时设置多个属性。

    eg: 同时设置 href 和 title 属性:
    
    $("button").click(function(){
      $("#w3s").attr({
        "href" : "http://www.w3school.com.cn/jquery",
        "title" : "W3School jQuery Tutorial"
      });
    });
  • attr() 的回调函数

    jQuery 方法 attr(),也提供回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。
    
    eg: 带有回调函数的 attr() 方法:
    $("button").click(function(){
      $("#w3s").attr("href", function(i,origValue){
        return origValue + "/jquery";
      });
    });

原文地址:https://www.cnblogs.com/ostrich-sunshine/p/8376442.html

时间: 2024-08-03 01:29:24

jquery - 设置/获取内容和属性的相关文章

jQuery - 设置获取内容和属性

获取选中select :$("#id option:selected").val(); 自定义radio:    $("input[name=sex][value="+data.sex+"]").attr("checked",true); 获取radio:           $("input[name='sex']:checked").val() 设置input不能编辑:$("#cashNum&

Jquery设置获取DOM元素属性

1, attr() $("button").click(function(){ $("#runoob").attr("href","www.96net.com.cn"); }); 2, 可以传递匿名函数 $("button").click(function(){ $("#runoob").attr("href", function(i,origValue){ retu

jquery获取内容和属性的方法

通过jquery如何捕获文本内容和属性? text(),html(),val()及attr(). attr()更具有普遍性,元素text属性和表单value属性,可以通过attr()操作. <!DOCTYPE html> <html> <head> <title>jquey获取内容和属性</title> <script type="text/javascript" src="../jquery.min.js&q

jQuery - 获取内容和属性:操作 HTML 元素和属性的强大方法

jQuery - 获取内容和属性 jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易. 获得内容 - text().html() 以及 val() 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记)

jquery 设置checkbox的checked属性 总是出问题

所使用的jquery版本为jquery-1.9.1,浏览器为Chrome 1.通过prop方法获取checked属性,获取的checked返回值为boolean,选中为true,否则为flase <input type="checkbox" id="selectAll" onclick="checkAll()">全选 function checkAll() {   var checkedOfAll=$("#selectAll

JQuery设置获取下拉菜单选项的值 多实例

分享下JQuery如何设置获取下拉菜单某个选项的值,多种方法,值得收藏. JQuery获取和设置Select选项 获取Select :获取select 选中的 text :$(“#ddlRegType”).find(“option:selected”).text();获取select选中的 value:$(“#ddlRegType “).val();获取select选中的索引:$(“#ddlRegType “).get(0).selectedIndex;设置select:设置select 选中的

jQuery如何获取指定type属性值的input元素

jQuery遍历input文本框并获取input的name属性值:因为input标签的type属性是多种多样的,例如text.radio.checkbox等,但是实际应用中往往需要获取某一类属性值的input元素,下面就通过实例简单介绍一下.代码实例如下: $("input:text", document.forms[0]).each(function(){alert(this.name)}); 以上代码可以获取type属性值为text的input元素,并且遍历弹出它们的name属性值

jQuery - 获取内容和属性

jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易. DOM = Document Object Model(文档对象模型) DOM 定义访问 HTML 和 XML 文档的标准: "W3C 文档对象模型独立于平台和语言的界面,允许程序和脚本动态访问和更新文档的内容.结构以及样式." 获得内容 - text()

jQuery中获取元素的属性方法attr()简单用法

语法结构:$(selector).attr(attribute), //selector为选择器名,attribute为指定要获取的元素的属性名 修改元素属性的$(selector).attr(attribute,value), //selector为选择器名,attribute为指定要获取的元素的属性名 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/T