当前位置:   article > 正文

【jQuery学习】jQuery内容文本值_jquery 定位元素内容

jquery 定位元素内容

1 普通元素内容 html()

相当于原生的innerHtml

获取内容html()

修改内容html("内容")

  1. <div>
  2. <span>123</span>
  3. </div>
  4. <script>
  5. $(function(){
  6. console.log($("div").html());
  7. $("div").html("456")
  8. })
  9. </script>

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

相当于原生中的innerText()

获取文本 text()

更改文本text("内容")

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

 案例:修改购物车小计

tips:

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

toFixed(n):保留n位小数

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

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="jquery.min.js"></script>
  9. <style>
  10. *{
  11. margin: 0;
  12. padding: 0;
  13. }
  14. body{
  15. font-size: 12px;
  16. color: #666;
  17. }
  18. a{
  19. text-decoration: none;
  20. color: #666;
  21. }
  22. em{
  23. font-style: normal;
  24. }
  25. .cart-warp{
  26. width: 1200px;
  27. margin: 100px auto;
  28. font-size: 12px;
  29. }
  30. .cart-thead{
  31. height: 32px;
  32. margin: 5px 0 10px;
  33. padding: 5px 0;
  34. line-height: 32px;
  35. background-color: #f3f3f3;
  36. border: 1px solid #e9e9e9;
  37. border-top: 0;
  38. /* 清除浮动 */
  39. overflow: hidden;
  40. }
  41. .cart-thead>div,
  42. .cart-item>div{
  43. float: left;
  44. }
  45. .cart-item{
  46. margin: 15px 0;
  47. padding-top: 14px;
  48. height: 160px;
  49. /* padding-bottom: 20px; */
  50. border-style: solid;
  51. border-width: 2px 1px 1px;
  52. border-color: #aaa #f1f1f1 #f1f1f1;
  53. overflow: hidden;
  54. }
  55. .t-checkbox,
  56. .p-checkbox
  57. {
  58. width: 122px;
  59. height: 18px;
  60. line-height: 18px;
  61. padding-top: 7px;
  62. padding-left: 11px;
  63. }
  64. .t-good{
  65. width: 400px;
  66. }
  67. .t-price{
  68. width: 120px;
  69. padding-right: 40px;
  70. text-align: right;
  71. }
  72. .t-num{
  73. width: 150px;
  74. text-align: center;
  75. }
  76. .t-sum{
  77. width: 100px;
  78. text-align: right;
  79. }
  80. .t-action{
  81. width: 130px;
  82. text-align: right;
  83. }
  84. .p-checkbox{
  85. width: 61px;
  86. }
  87. .p-img{
  88. float: left;
  89. border: 1px solid #ccc;
  90. }
  91. .p-msg{
  92. float: left;
  93. width: 210px;
  94. margin: 0 10px;
  95. }
  96. .p-goods{
  97. width: 565px;
  98. padding-top: 10px;
  99. }
  100. .p-price{
  101. width: 110px;
  102. }
  103. .p-num{
  104. width: 170px;
  105. }
  106. .p-num .decrement,
  107. .p-num .increment{
  108. float: left;
  109. width: 16px;
  110. height: 18px;
  111. line-height: 18px;
  112. text-align: center;
  113. border: 1px solid #cacbcb;
  114. background-color: #fff;
  115. color: #666;
  116. }
  117. .p-num .itxt{
  118. float: left;
  119. width: 42px;
  120. height: 18px;
  121. line-height: 18px;
  122. text-align: center;
  123. border: 1px solid #cacbcb;
  124. /* appearance: none; */
  125. }
  126. .p-sum{
  127. width: 145px;
  128. font-weight: 700;
  129. }
  130. .cart-floatbar{
  131. height: 50px;
  132. line-height: 50px;
  133. border: 1px solid #f0f0f0;
  134. }
  135. .select-all{
  136. float: left;
  137. height: 18px;
  138. line-height: 18px;
  139. padding: 16px 0 16px 9px;
  140. }
  141. .operation{
  142. float: left;
  143. width: 200px;
  144. margin-left: 40px;
  145. }
  146. .clear-all{
  147. font-weight: 700;
  148. margin: 0 20px;
  149. }
  150. .toolbar-right{
  151. float: right;
  152. }
  153. .toolbar-right>div{
  154. float: left;
  155. }
  156. .toolbar-right em{
  157. font-weight: 700;
  158. color: red;
  159. }
  160. .price-sum em{
  161. font-size: 18px;
  162. }
  163. .toolbar-right .price-sum{
  164. height: 50px;
  165. margin: 0 15px;
  166. }
  167. .toolbar-right .btn-area{
  168. width: 94px;
  169. height: 52px;
  170. line-height: 52px;
  171. text-align: center;
  172. font-size: 20px;
  173. font-weight: 700;
  174. background-color: red;
  175. color: #fff;
  176. }
  177. .check-cart-item{
  178. background-color: #fff4e8;
  179. }
  180. </style>
  181. <script>
  182. $(function(){
  183. $(".checkAll").change(function(){
  184. $(".j-checkbox, .checkAll").prop("checked", $(this).prop("checked"));
  185. })
  186. $(".j-checkbox").change(function(){
  187. if($(".j-checkbox:checked").length===$(".j-checkbox").length){
  188. $(".checkAll").prop("checked", true)
  189. }else{
  190. $(".checkAll").prop("checked", false)
  191. }
  192. })
  193. $(".increment").click(function(){
  194. var n = $(this).siblings(".itxt").val();
  195. n++;
  196. $(this).siblings(".itxt").val(n);
  197. var price = $(this).parents(".p-num").siblings(".p-price").text();
  198. price = price.substr(1);
  199. $(this).parents(".p-num").siblings(".p-sum").text("¥"+(price*n).toFixed(2))
  200. })
  201. $(".decrement").click(function(){
  202. var n = $(this).siblings(".itxt").val();
  203. if(n > 1){
  204. n--;
  205. $(this).siblings(".itxt").val(n);
  206. var price = $(this).parent().parent().siblings(".p-price").text();
  207. price = price.substr(1);
  208. $(this).parent().parent().siblings(".p-sum").text("¥"+(price*n).toFixed(2))
  209. }
  210. })
  211. $(".itxt").change(function(){
  212. var n = $(this).val();
  213. var price = $(this).parents(".p-num").siblings(".p-price").text();
  214. price = price.substr(1);
  215. $(this).parents(".p-num").siblings(".p-sum").text("¥"+(price*n).toFixed(2))
  216. })
  217. })
  218. </script>
  219. </head>
  220. <body>
  221. <div class="cart-warp">
  222. <!-- 头部全选模块 -->
  223. <div class="cart-thead">
  224. <div class="t-checkbox">
  225. <input type="checkbox" name="" id="" class="checkAll">全选
  226. </div>
  227. <div class="t-good">商品</div>
  228. <div class="t-price">单价</div>
  229. <div class="t-num">数量</div>
  230. <div class="t-sum">小计</div>
  231. <div class="t-action">操作</div>
  232. </div>
  233. <!-- 商品详细模块 -->
  234. <div class="cart-item-list">
  235. <div class="cart-item check-cart-item">
  236. <div class="p-checkbox">
  237. <input type="checkbox" name="" id="" class="j-checkbox">
  238. </div>
  239. <div class="p-goods">
  240. <div class="p-img">
  241. <img src="images/p1.jpg" alt="">
  242. </div>
  243. <div class="p-msg">【5本26.8元】经典儿童文学彩图青少版八十天环游地球中学生语文教学大纲</div>
  244. </div>
  245. <div class="p-price">¥12.60</div>
  246. <div class="p-num">
  247. <div class="quantity-form">
  248. <a href="javascript:;" class="decrement">-</a>
  249. <input type="text" class="itxt" value="1">
  250. <a href="javascript:;" class="increment">+</a>
  251. </div>
  252. </div>
  253. <div class="p-sum">¥12.60</div>
  254. <div class="p-action">
  255. <a href="javascript:;">删除</a>
  256. </div>
  257. </div>
  258. <div class="cart-item">
  259. <div class="p-checkbox">
  260. <input type="checkbox" name="" id="" class="j-checkbox">
  261. </div>
  262. <div class="p-goods">
  263. <div class="p-img">
  264. <img src="images/p2.jpg" alt="">
  265. </div>
  266. <div class="p-msg">【5本26.8元】经典儿童文学彩图青少版八十天环游地球中学生语文教学大纲</div>
  267. </div>
  268. <div class="p-price">¥12.60</div>
  269. <div class="p-num">
  270. <div class="quantity-form">
  271. <a href="javascript:;" class="decrement">-</a>
  272. <input type="text" class="itxt" value="1">
  273. <a href="javascript:;" class="increment">+</a>
  274. </div>
  275. </div>
  276. <div class="p-sum">¥12.60</div>
  277. <div class="p-action">
  278. <a href="javascript:;">删除</a>
  279. </div>
  280. </div>
  281. <div class="cart-item">
  282. <div class="p-checkbox">
  283. <input type="checkbox" name="" id="" class="j-checkbox">
  284. </div>
  285. <div class="p-goods">
  286. <div class="p-img">
  287. <img src="images/p3.jpg" alt="">
  288. </div>
  289. <div class="p-msg"></div>
  290. </div>
  291. <div class="p-price">¥12.60</div>
  292. <div class="p-num">
  293. <div class="quantity-form">
  294. <a href="javascript:;" class="decrement">-</a>
  295. <input type="text" class="itxt" value="1">
  296. <a href="javascript:;" class="increment">+</a>
  297. </div>
  298. </div>
  299. <div class="p-sum">¥12.60</div>
  300. <div class="p-action">
  301. <a href="javascript:;">删除</a>
  302. </div>
  303. </div>
  304. </div>
  305. <!-- 结算模块 -->
  306. <div class="cart-floatbar">
  307. <div class="select-all">
  308. <input type="checkbox" name="" id="" class="checkAll">全选
  309. </div>
  310. <div class="operation">
  311. <a href="javascript:;" class="remove-batch">删除选中的商品</a>
  312. <a href="javascript:;" class="clear-all">删除购物车</a>
  313. </div>
  314. <div class="toolbar-right">
  315. <div class="amount-sum">已经选<em>1</em>件商品</div>
  316. <div class="price-sum">总价:<em>¥12.60</em></div>
  317. <div class="btn-area">去结算</div>
  318. </div>
  319. </div>
  320. </div>
  321. </body>
  322. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/191704
推荐阅读
相关标签
  

闽ICP备14008679号