访问器属性与dom元素获取

访问器属性与dom元素获取

作业标题:0712作业
作业内容:

  1. 实例演示”访问器属性”,例如商品金额计算。
  2. 实例演示dom元素的获取,遍历与增删,方法不限于课堂上提及的,更多的可查阅mdn手册。

  • 访问器属性

    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>访问器属性</title>
    8. </head>
    9. <body>
    10. <script>
    11. let test = {
    12. data: {
    13. tops: "童装",
    14. numbers: 40,
    15. price: 50,
    16. },
    17. set numbers(numbers) {
    18. if (numbers > 0 && numbers != null) {
    19. return (this.data.numbers = numbers);
    20. }
    21. console.log("请输入合法数据");
    22. },
    23. set price(price) {
    24. if (price > 0 && price != null) {
    25. return (this.data.price = price);
    26. }
    27. console.log("请输入合法数据");
    28. },
    29. get numbers() {
    30. return this.data.numbers;
    31. },
    32. get price() {
    33. return this.data.price;
    34. },
    35. get tops() {
    36. return this.data.tops;
    37. },
    38. };
    39. test.price = 40;
    40. test.numbers = 10;
    41. console.log(test.tops + "总价为:" + test.numbers * test.price);
    42. </script>
    43. </body>
    44. </html>