【jQuery学习】jQuery内容文本值

这篇博客介绍了jQuery中获取和修改元素内容的方法,包括html()用于操作HTML内容,text()用于处理文本内容,以及val()针对表单元素的值进行操作。通过示例代码展示了如何实现全选、修改购物车商品数量并实时更新总价等功能,这些功能在前端开发中常见且重要。
摘要由CSDN通过智能技术生成

1 普通元素内容 html()

相当于原生的innerHtml

获取内容html()

修改内容html("内容")

<div>
        <span>123</span>
    </div>
    <script>
        $(function(){
            console.log($("div").html());
            $("div").html("456")
        })
    </script>

2 普通元素文本内容 text()

相当于原生中的innerText()

获取文本 text()

更改文本text("内容")

3 获取表单元素内容 val()

 案例:修改购物车小计

tips:

substr(1):截取第一个字符后面的字符串

toFixed(n):保留n位小数

parents("选择器"):选择祖先中对应的元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="jquery.min.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
            
        }
        body{
            font-size: 12px;
            color: #666;
        }
        a{
            text-decoration: none;
            color: #666;
        }
        em{
            font-style: normal;
        }
        .cart-warp{
            width: 1200px;
            margin: 100px auto;
            font-size: 12px;
        }
        .cart-thead{
            height: 32px;
            margin: 5px 0 10px;
            padding: 5px 0;
            line-height: 32px;
            background-color: #f3f3f3;
            border: 1px solid #e9e9e9;
            border-top: 0;
            /* 清除浮动 */
            overflow: hidden;
        }
        .cart-thead>div,
        .cart-item>div{
            float: left;
        }
        .cart-item{
            margin: 15px 0;
            padding-top: 14px;
            height: 160px;
            /* padding-bottom: 20px; */
            border-style: solid;
            border-width: 2px 1px 1px;
            border-color: #aaa #f1f1f1 #f1f1f1;
            overflow: hidden;
        }
        .t-checkbox,
        .p-checkbox
        {
            width: 122px;
            height: 18px;
            line-height: 18px;
            padding-top: 7px;
            padding-left: 11px;
        }
        .t-good{
            width: 400px;
        }
        .t-price{
            width: 120px;
            padding-right: 40px;
            text-align: right;
        }
        .t-num{
            width: 150px;
            text-align: center;
        }
        .t-sum{
            width: 100px;
            text-align: right;
        }
        .t-action{
            width: 130px;
            text-align: right;
        }
        .p-checkbox{
            width: 61px;
        }
        .p-img{
            float: left;
            border: 1px solid #ccc;
        }
        .p-msg{
            float: left;
            width: 210px;
            margin: 0 10px;
        }
        .p-goods{
            width: 565px;
            padding-top: 10px;
        }
        .p-price{
            width: 110px;
        }
        .p-num{
            width: 170px;
           
        }
        .p-num .decrement,
        .p-num .increment{
            float: left;
            width: 16px;
            height: 18px;
            line-height: 18px;
            text-align: center;
            border: 1px solid #cacbcb;
            background-color: #fff;
            color: #666;

        }
        .p-num .itxt{
            float: left;
            width: 42px;
            height: 18px;
            line-height: 18px;
            text-align: center;
            border: 1px solid #cacbcb;
            /* appearance: none; */
        }
        .p-sum{
            width: 145px;
            font-weight: 700;
        }
        .cart-floatbar{
            height: 50px;
            line-height: 50px;
            border: 1px solid #f0f0f0;
        }
        .select-all{
            float: left;
            height: 18px;
            line-height: 18px;
            padding: 16px 0 16px 9px;
        }
        .operation{
            float: left;
            width: 200px;
            margin-left: 40px;
        }
        .clear-all{
            font-weight: 700;
            margin: 0 20px;
        }
        .toolbar-right{
            float: right;
        }
        .toolbar-right>div{
            float: left;
        }
        .toolbar-right em{
            font-weight: 700;
            color: red;
        }
        .price-sum em{
            font-size: 18px;
        }
        .toolbar-right .price-sum{
            height: 50px;
            margin: 0 15px;
        }
        .toolbar-right .btn-area{
            width: 94px;
            height: 52px;
            line-height: 52px;
            text-align: center;
            font-size: 20px;
            font-weight: 700;
            background-color: red;
            color: #fff;
        }
        .check-cart-item{
            background-color: #fff4e8;
        }
    </style>
    <script>
        $(function(){
            $(".checkAll").change(function(){
                $(".j-checkbox, .checkAll").prop("checked", $(this).prop("checked"));

            })
            $(".j-checkbox").change(function(){
                if($(".j-checkbox:checked").length===$(".j-checkbox").length){
                    $(".checkAll").prop("checked", true)
                }else{
                    $(".checkAll").prop("checked", false)
                }
            })
            $(".increment").click(function(){
                var n = $(this).siblings(".itxt").val();
                n++;
                $(this).siblings(".itxt").val(n);
                var price = $(this).parents(".p-num").siblings(".p-price").text();
                price = price.substr(1);
                $(this).parents(".p-num").siblings(".p-sum").text("¥"+(price*n).toFixed(2))
            })
            $(".decrement").click(function(){
                var n = $(this).siblings(".itxt").val();
                if(n > 1){
                    n--;
                    $(this).siblings(".itxt").val(n);
                    var price = $(this).parent().parent().siblings(".p-price").text();
                    price = price.substr(1);
                    $(this).parent().parent().siblings(".p-sum").text("¥"+(price*n).toFixed(2))
                }
                
            })

            $(".itxt").change(function(){
                var n = $(this).val();
                var price = $(this).parents(".p-num").siblings(".p-price").text();
                price = price.substr(1);
                $(this).parents(".p-num").siblings(".p-sum").text("¥"+(price*n).toFixed(2))

            })
        })
    </script>
</head>
<body>
    <div class="cart-warp">
        <!-- 头部全选模块 -->
        <div class="cart-thead">
            <div class="t-checkbox">
                <input type="checkbox" name="" id="" class="checkAll">全选
            </div>
            <div class="t-good">商品</div>
            <div class="t-price">单价</div>
            <div class="t-num">数量</div>
            <div class="t-sum">小计</div>
            <div class="t-action">操作</div>
        </div>
        <!-- 商品详细模块 -->
        <div class="cart-item-list">
            <div class="cart-item check-cart-item">
                <div class="p-checkbox">
                    <input type="checkbox" name="" id="" class="j-checkbox">
                </div>
                <div class="p-goods">
                    <div class="p-img">
                        <img src="images/p1.jpg" alt="">
                    </div>
                    <div class="p-msg">【5本26.8元】经典儿童文学彩图青少版八十天环游地球中学生语文教学大纲</div>
                </div>
                <div class="p-price">¥12.60</div>
                <div class="p-num">
                    <div class="quantity-form">
                        <a href="javascript:;" class="decrement">-</a>
                        <input type="text" class="itxt" value="1">
                        <a href="javascript:;" class="increment">+</a>
                    </div>
                </div>
                <div class="p-sum">¥12.60</div>
                <div class="p-action">
                    <a href="javascript:;">删除</a>
                </div>
            </div>
            <div class="cart-item">
                <div class="p-checkbox">
                    <input type="checkbox" name="" id="" class="j-checkbox">
                </div>
                <div class="p-goods">
                    <div class="p-img">
                        <img src="images/p2.jpg" alt="">
                    </div>
                    <div class="p-msg">【5本26.8元】经典儿童文学彩图青少版八十天环游地球中学生语文教学大纲</div>
                </div>
                <div class="p-price">¥12.60</div>
                <div class="p-num">
                    <div class="quantity-form">
                        <a href="javascript:;" class="decrement">-</a>
                        <input type="text" class="itxt" value="1">
                        <a href="javascript:;" class="increment">+</a>
                    </div>
                </div>
                <div class="p-sum">¥12.60</div>
                <div class="p-action">
                    <a href="javascript:;">删除</a>
                </div>
            </div>
            <div class="cart-item">
                <div class="p-checkbox">
                    <input type="checkbox" name="" id="" class="j-checkbox">
                </div>
                <div class="p-goods">
                    <div class="p-img">
                        <img src="images/p3.jpg" alt="">
                    </div>
                    <div class="p-msg"></div>
                </div>
                <div class="p-price">¥12.60</div>
                <div class="p-num">
                    <div class="quantity-form">
                        <a href="javascript:;" class="decrement">-</a>
                        <input type="text" class="itxt" value="1">
                        <a href="javascript:;" class="increment">+</a>
                    </div>
                </div>
                <div class="p-sum">¥12.60</div>
                <div class="p-action">
                    <a href="javascript:;">删除</a>
                </div>
            </div>
        </div>
        <!-- 结算模块 -->
        <div class="cart-floatbar">
            <div class="select-all">
                <input type="checkbox" name="" id="" class="checkAll">全选
            </div>
            <div class="operation">
                <a href="javascript:;" class="remove-batch">删除选中的商品</a>
                <a href="javascript:;" class="clear-all">删除购物车</a>
            </div>
            <div class="toolbar-right">
                <div class="amount-sum">已经选<em>1</em>件商品</div>
                <div class="price-sum">总价:<em>¥12.60</em></div>
                <div class="btn-area">去结算</div>
            </div>

        </div>
    </div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值