吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1531|回复: 5
收起左侧

[已解决] 求助前端大佬,为什么这里点击按钮点不动,是啥问题啊

  [复制链接]
拨Q 发表于 2023-4-11 10:41
40吾爱币
本帖最后由 拨Q 于 2023-4-11 18:57 编辑

[HTML] 纯文本查看 复制代码
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>生活常识类教程 - 吴老师博客</title>
<link rel="stylesheet" href="/wp-content/themes/life-1/style.css" type="text/css" />
<script type='text/javascript' src='https://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js'></script>

</head>
<body class="archive category category-shenghuo category-2">
<header class="header">
        <div class="container">
                <div class="logo"><a href="/" title=""><img src="/wp-content/uploads/2023/04/logo.png" ></a></div>
       <div class="sitenav"><ul id="sitenav" class="sitenav"><li id="menu-item-17" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-17"><a href="xq/">心情</a>
<ul class="sub-menu">
        <li id="menu-item-18" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-182923"><a href="/2022/">2022年</a></li>

</ul>
</li>
<li id="menu-item-29" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-9"><a href="/sb/">随笔</a>

</li>
<li id="menu-item-11" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-41"><a href="/jczj/">教程总结</a>

</li>
</ul></div>
                <span class="sitenav-on"><i class="fa"></i></span>
                <span class="sitenav-mask"></span>
        </div>
</header><div class="clearfix"></div><div class="focusbox">

  <style>
    .text {
      overflow: hidden;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }
    .text-container.expanded .text {
      -webkit-line-clamp: unset;
    }
    .text-container.expanded .expand-button {
      display: inline-block;
    }
  </style>
    <script>
    const textContainer = document.querySelector('.text-container');
    const text = document.querySelector('.text');
    const expandButton = document.querySelector('.expand-button');
    
    if (text.offsetHeight > textContainer.offsetHeight) {
      expandButton.style.display = 'inline-block';
    }
    
    expandButton.addEventListener('click', () => {
      textContainer.classList.toggle('expanded');
      if (textContainer.classList.contains('expanded')) {
        expandButton.textContent = '收起';
      } else {
        expandButton.textContent = '展开';
      }
    });
  </script>
        <div class="container">
            <h1 class="focusbox-title">生活常识类教程</h1>

  <div class="text-container">
    <p class="text">
      “闻春雷而动,濯春雨而发”,因形美味鲜气清,春笋被誉为“蔬中第一珍”。近日,患有十二指肠溃疡的王先生却因多日食用春笋发生呕血、晕厥。医生表示,春笋含有粗纤维难以消化,大量食用会刺激肠胃,尤其是胃病患者,容易造成消化道出血。北京中医药大学东直门医院通州院区脾胃病科主治医师茹淑瑛介绍,除春笋外,辛辣刺激、容易引发胀气、不易消化以及生冷食物等都不适宜肠胃不适人群食用。“咖啡、浓茶、酒、辣椒、韭菜等属于辛辣刺激食物,土豆、红薯、芋头、南瓜、板栗、豆浆等属于易胀气食物,年糕、粽子、汤圆等属于不易消化食物。此外,油炸食品、冰镇食品、甜点等也要少吃。”茹淑瑛解释道。“春季养胃以食养为主,也要避免冷热交替导致的胃肠型感冒。此外,保持良好情绪、养成分餐进食习惯也有益。”最后,茹淑瑛介绍几款春季养胃药膳。
    </p>
    <button class="expand-button">展开</button>
  </div>

    </div>



</div>

<section class="container">
<div class="excerpts-wrapper"><div class="excerpts">
     
<article class="excerpt excerpt-c3"><a href="/415.html">DIY组装电脑基础教程</a></article>
   
</div></div>

</section> 
<footer class="footer">All Copyright Rights Reserved.Wu <a target="_blank" href="http://beian.miit.gov.cn">浙ICP备111号</a></footer>
<div style="display:none"></div>
<script type="text/javascript" src="wp-content/themes/life-1/js/main.js"></script>
<script type="text/javascript">
$(document).ready(function() { 
        $.fn.postLike = function() {
                if ($(this).hasClass('done')) {
                        alert('谢谢点赞,但是你已经点过了不要再点了!');
                        return false;
                } else {
                        $(this).addClass('done');
                        var id = $(this).data("id"),
                        action = $(this).data('action'),
                        rateHolder = $(this).children('.count');
                        var ajax_data = {
                                action: "bigfa_like",
                                um_id: id,
                                um_action: action
                        };
                        $.post("/wp-admin/admin-ajax.php", ajax_data, function(data) {
                                $(rateHolder).html(data);
                        });
                        return false;
                }
        };
        $(document).on("click", ".favorite", function() {
                $(this).postLike();
        });
});

</script>

<script type='text/javascript' src='wp-includes/js/jquery/jquery.js'></script>
<script type='text/javascript' src='wp-includes/js/jquery/jquery-migrate.min.js'></script>

<script type='text/javascript' src='wp-content/plugins/zeno-report-comments/js/ajax.js'></script>
<script type='text/javascript' src='wp-includes/js/wp-embed.min.js'></script>
</body>
</html>


不知道咋回事,是冲突了吗,这个展开点不动,大佬帮忙看看

最佳答案

查看完整内容

这段代码目标的div在script下面,导致dom没有获取到该div读取了null,程序出错后续监听没有运行,将script移到div下面即可,或者让script在div加载完毕后执行,$(document).ready(function(){})

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

T4DNA 发表于 2023-4-11 10:41
[HTML] 纯文本查看 复制代码
const textContainer = document.querySelector('.text-container');

这段代码目标的div在script下面,导致dom没有获取到该div读取了null,程序出错后续监听没有运行,将script移到div下面即可,或者让script在div加载完毕后执行,$(document).ready(function(){})

免费评分

参与人数 1吾爱币 +2 热心值 +1 收起 理由
拨Q + 2 + 1 阿里嘎多

查看全部评分

T4DNA 发表于 2023-4-11 10:53
你打开控制台看一眼啊,报错了
[HTML] 纯文本查看 复制代码
<!DOCTYPE HTML>
<html>

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>生活常识类教程 - 吴老师博客</title>
  <link rel="stylesheet" href="/wp-content/themes/life-1/style.css" type="text/css" />
  <script type='text/javascript' src='https://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js'></script>

</head>

<body class="archive category category-shenghuo category-2">
  <header class="header">
    <div class="container">
      <div class="logo"><a href="/" title=""><img src="/wp-content/uploads/2023/04/logo.png"></a></div>
      <div class="sitenav">
        <ul id="sitenav" class="sitenav">
          <li id="menu-item-17"
            class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-17"><a
              href="xq/">心情</a>
            <ul class="sub-menu">
              <li id="menu-item-18" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-182923"><a
                  href="/2022/">2022年</a></li>

            </ul>
          </li>
          <li id="menu-item-29"
            class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-9"><a
              href="/sb/">随笔</a>

          </li>
          <li id="menu-item-11"
            class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-41"><a
              href="/jczj/">教程总结</a>

          </li>
        </ul>
      </div>
      <span class="sitenav-on"><i class="fa"></i></span>
      <span class="sitenav-mask"></span>
    </div>
  </header>
  <div class="clearfix"></div>
  <div class="focusbox">

    <style>
      .text {
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
      }

      .text-container.expanded .text {
        -webkit-line-clamp: unset;
      }

      .text-container.expanded .expand-button {
        display: inline-block;
      }
    </style>
    <div class="container">
      <h1 class="focusbox-title">生活常识类教程</h1>

      <div class="text-container">
        <p class="text">
          “闻春雷而动,濯春雨而发”,因形美味鲜气清,春笋被誉为“蔬中第一珍”。近日,患有十二指肠溃疡的王先生却因多日食用春笋发生呕血、晕厥。医生表示,春笋含有粗纤维难以消化,大量食用会刺激肠胃,尤其是胃病患者,容易造成消化道出血。北京中医药大学东直门医院通州院区脾胃病科主治医师茹淑瑛介绍,除春笋外,辛辣刺激、容易引发胀气、不易消化以及生冷食物等都不适宜肠胃不适人群食用。“咖啡、浓茶、酒、辣椒、韭菜等属于辛辣刺激食物,土豆、红薯、芋头、南瓜、板栗、豆浆等属于易胀气食物,年糕、粽子、汤圆等属于不易消化食物。此外,油炸食品、冰镇食品、甜点等也要少吃。”茹淑瑛解释道。“春季养胃以食养为主,也要避免冷热交替导致的胃肠型感冒。此外,保持良好情绪、养成分餐进食习惯也有益。”最后,茹淑瑛介绍几款春季养胃药膳。
        </p>
        <button class="expand-button">展开</button>
      </div>

    </div>



  </div>

  <section class="container">
    <div class="excerpts-wrapper">
      <div class="excerpts">

        <article class="excerpt excerpt-c3"><a href="/415.html">DIY组装电脑基础教程</a></article>

      </div>
    </div>

  </section>
  <footer class="footer">All Copyright Rights Reserved.Wu <a target="_blank"
      href="http://beian.miit.gov.cn">浙ICP备111号</a></footer>
  <div style="display:none"></div>
  <script type="text/javascript" src="wp-content/themes/life-1/js/main.js"></script>
  <script type="text/javascript">
    $(document).ready(function () {
      $.fn.postLike = function () {
        if ($(this).hasClass('done')) {
          alert('谢谢点赞,但是你已经点过了不要再点了!');
          return false;
        } else {
          $(this).addClass('done');
          var id = $(this).data("id"),
            action = $(this).data('action'),
            rateHolder = $(this).children('.count');
          var ajax_data = {
            action: "bigfa_like",
            um_id: id,
            um_action: action
          };
          $.post("/wp-admin/admin-ajax.php", ajax_data, function (data) {
            $(rateHolder).html(data);
          });
          return false;
        }
      };
      $(document).on("click", ".favorite", function () {
        $(this).postLike();
      });
    });

  </script>
  <script>
    const textContainer = document.querySelector('.text-container');
    const text = document.querySelector('.text');
    const expandButton = document.querySelector('.expand-button');

    if (text.offsetHeight > textContainer.offsetHeight) {
      expandButton.style.display = 'inline-block';
    }

    expandButton.addEventListener('click', () => {
      textContainer.classList.toggle('expanded');
      if (textContainer.classList.contains('expanded')) {
        expandButton.textContent = '收起';
      } else {
        expandButton.textContent = '展开';
      }
    });
  </script>
  <script type='text/javascript' src='wp-includes/js/jquery/jquery.js'></script>
  <script type='text/javascript' src='wp-includes/js/jquery/jquery-migrate.min.js'></script>

  <script type='text/javascript' src='wp-content/plugins/zeno-report-comments/js/ajax.js'></script>
  <script type='text/javascript' src='wp-includes/js/wp-embed.min.js'></script>
</body>

</html>
三岁就会写BUG 发表于 2023-4-11 11:02
页面dom树加载完成再去获取dom节点,或者把获取元素的script放最下面,楼上正解
我家有条大大狗 发表于 2023-4-11 11:14
楼上正解
contest 发表于 2023-4-11 16:32
本帖最后由 contest 于 2023-4-11 16:40 编辑

[HTML] 纯文本查看 复制代码
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>生活常识类教程 - 吴老师博客</title>
<link rel="stylesheet" href="/wp-content/themes/life-1/style.css" type="text/css" />
<script type='text/javascript' src='https://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js'></script>
 
</head>
<body class="archive category category-shenghuo category-2">
<header class="header">
        <div class="container">
                <div class="logo"><a href="/" title=""><img src="/wp-content/uploads/2023/04/logo.png" ></a></div>
       <div class="sitenav"><ul id="sitenav" class="sitenav"><li id="menu-item-17" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-17"><a href="xq/">心情</a>
<ul class="sub-menu">
        <li id="menu-item-18" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-182923"><a href="/2022/">2022年</a></li>
 
</ul>
</li>
<li id="menu-item-29" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-9"><a href="/sb/">随笔</a>
 
</li>
<li id="menu-item-11" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-41"><a href="/jczj/">教程总结</a>
 
</li>
</ul></div>
                <span class="sitenav-on"><i class="fa"></i></span>
                <span class="sitenav-mask"></span>
        </div>
</header><div class="clearfix"></div><div class="focusbox">
 
  <style>
    .text {
      overflow: hidden;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }
    .text-container.expanded .text {
      -webkit-line-clamp: unset;
    }
    .text-container.expanded .expand-button {
      display: inline-block;
    }
  </style>
    
        <div class="container">
            <h1 class="focusbox-title">生活常识类教程</h1>
 
  <div class="text-container">
    <p class="text">
      “闻春雷而动,濯春雨而发”,因形美味鲜气清,春笋被誉为“蔬中第一珍”。近日,患有十二指肠溃疡的王先生却因多日食用春笋发生呕血、晕厥。医生表示,春笋含有粗纤维难以消化,大量食用会刺激肠胃,尤其是胃病患者,容易造成消化道出血。北京中医药大学东直门医院通州院区脾胃病科主治医师茹淑瑛介绍,除春笋外,辛辣刺激、容易引发胀气、不易消化以及生冷食物等都不适宜肠胃不适人群食用。“咖啡、浓茶、酒、辣椒、韭菜等属于辛辣刺激食物,土豆、红薯、芋头、南瓜、板栗、豆浆等属于易胀气食物,年糕、粽子、汤圆等属于不易消化食物。此外,油炸食品、冰镇食品、甜点等也要少吃。”茹淑瑛解释道。“春季养胃以食养为主,也要避免冷热交替导致的胃肠型感冒。此外,保持良好情绪、养成分餐进食习惯也有益。”最后,茹淑瑛介绍几款春季养胃药膳。
    </p>
    <button class="expand-button">展开</button>
  </div>
  <script>
    const textContainer = document.querySelector('.text-container');
    const text = document.querySelector('.text');
    const expandButton = document.querySelector('.expand-button');
    console.log(textContainer);
    if (text.offsetHeight > textContainer.offsetHeight) {
      expandButton.style.display = 'inline-block';
    }
     
    expandButton.addEventListener('click', () => {
      textContainer.classList.toggle('expanded');
      if (textContainer.classList.contains('expanded')) {
        expandButton.textContent = '收起';
      } else {
        expandButton.textContent = '展开';
      }
    });
  </script>
    </div>
 
 
 
</div>
 
<section class="container">
<div class="excerpts-wrapper"><div class="excerpts">
      
<article class="excerpt excerpt-c3"><a href="/415.html">DIY组装电脑基础教程</a></article>
    
</div></div>
 
</section> 
<footer class="footer">All Copyright Rights Reserved.Wu <a target="_blank" >浙ICP备111号</a></footer>
<div style="display:none"></div>
<script type="text/javascript" src="wp-content/themes/life-1/js/main.js"></script>
<script type="text/javascript">
$(document).ready(function() { 
        $.fn.postLike = function() {
                if ($(this).hasClass('done')) {
                        alert('谢谢点赞,但是你已经点过了不要再点了!');
                        return false;
                } else {
                        $(this).addClass('done');
                        var id = $(this).data("id"),
                        action = $(this).data('action'),
                        rateHolder = $(this).children('.count');
                        var ajax_data = {
                                action: "bigfa_like",
                                um_id: id,
                                um_action: action
                        };
                        $.post("/wp-admin/admin-ajax.php", ajax_data, function(data) {
                                $(rateHolder).html(data);
                        });
                        return false;
                }
        };
        $(document).on("click", ".favorite", function() {
                $(this).postLike();
        });
});
 
</script>
 
<script type='text/javascript' src='wp-includes/js/jquery/jquery.js'></script>
<script type='text/javascript' src='wp-includes/js/jquery/jquery-migrate.min.js'></script>
 
<script type='text/javascript' src='wp-content/plugins/zeno-report-comments/js/ajax.js'></script>
<script type='text/javascript' src='wp-includes/js/wp-embed.min.js'></script>
</body>
</html>

改成这样就可以了,这是第一种方法。
开始排查问题发现const textContainer = document.querySelector('.text-container');语句未获取到元素为null,进而判断原因,由于HTML执行规律,一般将script语句放置在html其元素下方,本次无法展开即为执行script语句时未能获取对应元素。进而出现第二种解决方法,使用onload等待页面加载完成后在执行对应script语句

免费评分

参与人数 1吾爱币 +2 热心值 +1 收起 理由
拨Q + 2 + 1 谢谢@Thanks!

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 00:54

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表