jquery中val(),text(),html()三个方法的区别

三个方法都是jquery中用于获取或者设置内容

(1)val()主要针对的对象是表单元素(input)的value属性

<input id="valtest" type="text" value="我是val()">,通过$("#valtest").val()获取到"我是val()"。

(2)text()获取或者设置对象的文本内容,不包括html标签,包括子节点的文本

<p id="ptest">段落中的<b>粗体</b>文本。</p>,通过$("#ptest").text()获取到"段落中的粗体文本"。

(3)html()获取或者设置对象的内容,包括html标签

(2)中通过$("#ptest").html()获取到"段落中的<b>粗体</b>文本"。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,还有很多其他的方法可以使用jquery实现上述功能。以下是另一种实现方式: ```html <!DOCTYPE html> <html> <head> <title>成绩判断</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style type="text/css"> body { background-color: white; } .gray { background-color: gray; } .blue { background-color: #87CEFA; } .yellow { background-color: #FFFFE0; } .red { background-color: #FFC0CB; } </style> </head> <body> <p>请输入三科成绩:</p> <input id="score1" type="number" placeholder="第一科成绩"> <input id="score2" type="number" placeholder="第二科成绩"> <input id="score3" type="number" placeholder="第三科成绩"> <button id="submit">提交</button> <script type="text/javascript"> $(document).ready(function() { $("#submit").click(function() { var score1 = parseInt($("#score1").val()); var score2 = parseInt($("#score2").val()); var score3 = parseInt($("#score3").val()); if (score1 < 60 && score2 < 60 && score3 < 60) { $("body").removeClass().addClass("gray"); alert("请努力吧"); } else if ((score1 < 60 && score2 >= 60 && score3 >= 60) || (score1 >= 60 && score2 < 60 && score3 >= 60) || (score1 >= 60 && score2 >= 60 && score3 < 60)) { $("body").removeClass().addClass("blue"); alert("还需努力"); } else if (score1 >= 60 && score2 >= 60 && score3 >= 60 && (score1 < 85 || score2 < 85 || score3 < 85)) { $("body").removeClass().addClass("yellow"); alert("马马虎虎"); } else { $("body").removeClass().addClass("red"); alert("你成绩优异"); } }); }); </script> </body> </html> ``` 在这个代码,我们定义了四个类名,分别对应了不同的背景色。当点击提交按钮时,我们使用jquery根据成绩的情况,移除当前body元素的所有类名,并添加对应的类名,从而改变网页的背景色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值