【问题标题】:How can I select clicked content?如何选择点击的内容?
【发布时间】:2018-04-30 06:48:31
【问题描述】:

我如何选择点击的内容?现在我正在制作一个系统,当我点击一个按钮时,数据显示在nav标签中。 我在views.py中写了代码

class TopView(TemplateView):
    model = Data
    template_name = 'index.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['data'] = Data.objects.all()
        return context

当我点击一个按钮时,所有数据都显示在 index.html 的 nav 标签中。我只想在 nav 标签中显示点击的数据。我想我是这样写的,但是我的代码有什么问题?我应该怎么做解决这个问题?

<html lang="ja">
    <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<head>
    <meta charset="UTF-8">
    <title>HOMEPAGE</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
   nav {
    width: 312px;
    height: 100%;
    transition: all 0.2s;
    transform: translate(312px);
    position: fixed;
    top: 0;
    right: 0;
    z-index: 1000;
    background-color: #FFF;
   }
   nav.open {
    transform: translate(0);
   }
  </style>
</head>

<script>
$(function(){
  $('.btn_menu').click(function(){
    $(this).next('nav').toggleClass('open');
  });
})
</script>
<body>

    <section id="center">
                        <button type="button" class="btn_menu">
                            AAA
                        </button>
                        <button type="button" class="btn_menu">
                            BBB
                        </button>
                        <button type="button" class="btn_menu">
                            CCC
                        </button>

    </section>

    <nav>
        <h2>HOMEPAGE</h2>
        <!---->
                <h4>AAA</h4>
                <p>aaa</p>
        <!---->
                <h4>BBB</h4>
                <p>bbb</p>
        <!---->
                <h4>CCC</h4>
                <p>ccc</p>
        <!---->
    </nav>

</body>
</html>

【问题讨论】:

  • 你能提供一个同样有效的sn-p吗?
  • user9673470 检查我编辑的解决方案

标签: python jquery html django


【解决方案1】:

看到渲染的 HTML 后,您需要执行以下操作:-

$(document).ready(function(){
  $('nav h4, nav p').hide();
  $('.btn_menu').click(function(){
    var obj = $(this);
    $('nav h4, nav p').hide();
    $('h4, p').each(function(){
      if($.trim($(this).text().toLowerCase()) == $.trim(obj.text().toLowerCase())){
        $(this).show();
      }
    });
  });
});

工作 sn-p:-

$(document).ready(function(){
  $('nav h4, nav p').hide();
  $('.btn_menu').click(function(){
    var obj = $(this);
    $('nav h4, nav p').hide();
    $('h4, p').each(function(){
      if($.trim($(this).text().toLowerCase()) == $.trim(obj.text().toLowerCase())){
        $(this).show();
      }
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="center">
  <button type="button" class="btn_menu">
      AAA
  </button>
  <button type="button" class="btn_menu">
      BBB
  </button>
  <button type="button" class="btn_menu">
      CCC
  </button>

</section>

<nav>
<h2>HOMEPAGE</h2>
<!---->
  <h4>AAA</h4>
  <p>aaa</p>
<!---->
  <h4>BBB</h4>
  <p>bbb</p>
<!---->
  <h4>CCC</h4>
  <p>ccc</p>
<!---->
</nav>

【讨论】:

  • user9673470 很高兴为您提供帮助:):)
【解决方案2】:

问题出在这一行$(this).next('nav').toggleClass('open'); next() 用于查找直接兄弟。

要解决此问题,您应该使用

$('nav').toggleClass('open'); 

另外,您需要重组代码,在 &lt;/body&gt; 之前将 js/jquery 加载到 Head 或 Body(首选)中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2015-06-16
    • 2022-12-20
    • 2016-11-08
    • 1970-01-01
    相关资源
    最近更新 更多