html 如何在使用if语句后隐藏对象[关闭]

6qfn3psc  于 2023-11-15  发布在  其他
关注(0)|答案(1)|浏览(145)

已关闭。此问题需要details or clarity。目前不接受回答。
**要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

6天前关闭
Improve this question
因此,我试图使一个项目消失时,以往任何时候的本地存储carted = 2和存储确实工作,我总是只是得到一个错误时,试图隐藏和对象,我会apreciate任何帮助,因为这是为学校谢谢,我真的需要解决这个问题,任何帮助是apreciated

  1. <html>
  2. <link rel="stylesheet" href="style.css">
  3. <Head>
  4. <title> Clear Shop | cart
  5. </title>
  6. <link rel="icon" type="image/x-icon" href="fav.png">
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@500&display=swap" rel="stylesheet">
  10. </Head>
  11. <style>
  12. ul {
  13. list-style-type: none;
  14. margin: 0;
  15. padding: 0;
  16. overflow: hidden;
  17. background-color: #489FB5;
  18. }
  19. li {
  20. float: left;
  21. }
  22. li a {
  23. display: block;
  24. color: white;
  25. text-align: center;
  26. padding: 14px 16px;
  27. text-decoration: none;
  28. }
  29. li a:hover {
  30. background-color: #1D4049;
  31. border: none;
  32. color: white;
  33. text-align: center;
  34. opacity: 0.6;
  35. transition: 0.8s;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <script>
  41. var Carted;
  42. var mycart = document.getElementById('Itemincart');
  43. Carted = localStorage.getItem('Carted')
  44. if (Carted == 2) {
  45. mycart.style.display = 'none';
  46. console.log("hidden");
  47. }
  48. function Addtocart() {
  49. if (Carted == 2) {
  50. console.log("hidden");
  51. mycart.style.display = 'none';
  52. }
  53. console.log("Unadded");
  54. Carted = 2;
  55. localStorage.setItem('Carted',Carted);
  56. }
  57. </script>
  58. <ul>
  59. <li class="pfp"><a href="#pfp"> <img src="pfp.jpg" alt="pfp" width="50"
  60. height="50"></a></li>
  61. <li class="cart"><a href="cart.html"> <img src="cart.png" alt="cart" width="50"
  62. height="50"></a></li>
  63. <li class="Home"><a class="active" href="home.html">CLEAR SHOP</a></li>
  64. <li class="shop"><a href="shop.html">Shop</a></li>
  65. <li class="cont"><a href="contact.html">Contact</a></li>
  66. <li class="about"><a href="about.html">About</a></li>
  67. </ul>
  68. <div id="Itemincart">
  69. <p class="boxtext"id="rcorners1">Item<br><p2 style="font-size: 15;">$100</p2> <button onclick="Addtocart()">X </button></p>
  70. </div>
  71. </h1>
  72. </html>

字符串
mycart.style.display = 'none';根本不起作用

bgtovc5b

bgtovc5b1#

你关于var mycard的声明是get a Element that doesn't exists,脚本页面是程序化的,所以试着把你的js脚本放在元素后面.试试:

  1. <html>
  2. <link rel="stylesheet" href="style.css">
  3. <Head>
  4. <title> Clear Shop | cart
  5. </title>
  6. <link rel="icon" type="image/x-icon" href="fav.png">
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@500&display=swap" rel="stylesheet">
  10. <style>
  11. ul {
  12. list-style-type: none;
  13. margin: 0;
  14. padding: 0;
  15. overflow: hidden;
  16. background-color: #489FB5;
  17. }
  18. li {
  19. float: left;
  20. }
  21. li a {
  22. display: block;
  23. color: white;
  24. text-align: center;
  25. padding: 14px 16px;
  26. text-decoration: none;
  27. }
  28. li a:hover {
  29. background-color: #1D4049;
  30. border: none;
  31. color: white;
  32. text-align: center;
  33. opacity: 0.6;
  34. transition: 0.8s;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <ul>
  40. <li class="pfp"><a href="#pfp"> <img src="pfp.jpg" alt="pfp" width="50"
  41. height="50"></a></li>
  42. <li class="cart"><a href="cart.html"> <img src="cart.png" alt="cart" width="50"
  43. height="50"></a></li>
  44. <li class="Home"><a class="active" href="home.html">CLEAR SHOP</a></li>
  45. <li class="shop"><a href="shop.html">Shop</a></li>
  46. <li class="cont"><a href="contact.html">Contact</a></li>
  47. <li class="about"><a href="about.html">About</a></li>
  48. </ul>
  49. <div id="Itemincart">
  50. <p class="boxtext"id="rcorners1">Item<br><p2 style="font-size: 15;">$100</p2> <button onclick="Addtocart()">X </button></p>
  51. </div>
  52. <script>
  53. var mycart = document.getElementById('Itemincart');
  54. Carted = localStorage.getItem('Carted')
  55. if (Carted === 2) {
  56. mycart.style.display = 'none';
  57. console.log("hidden 1");
  58. }
  59. function Addtocart() {
  60. if (Carted === 2) {
  61. console.log("hidden 2");
  62. mycart.style.display = 'none';
  63. }
  64. console.log("Unadded");
  65. Carted = 2;
  66. localStorage.setItem('Carted',Carted);
  67. }
  68. </script>
  69. </body>
  70. </html>

字符串
别忘了,JS中的比较,用=

展开查看全部

相关问题