javascript - 我希望一个 <li> 在单击时隐藏另一个 <li> 在同一个 <ul>

标签 javascript php jquery html css

如标题所述,我在我的网站上创建了一个“常见问题解答”部分,人们可以在其中单击一个字形图标,它会显示文本。这是我的代码

<div class="tab-content">
    <div id="home" class="tab-pane fade in active ">
        <h3>Product Specs</h3>
        <ul id="questions">
            <li><span id="symbol"><i class="fa fa-gg"></i></span> Do I have to use one of your templates for my vinyl banner?</li>
            <li id="subtext">While we have created templates for you to use as a starting point,  if you don’t want to use them and would like to design your own banner, you have three additional options. First, you can choose the blank template and start designing from scratch on our design tool. Second, you can upload your own files to make a banner. Lastly, you can contact us and have us design something for you.</li>
        </ul>
    </div>
</div>

当用户点击 <span id="symbol">它将显示/隐藏

<li id="subtext">

最佳答案

您可以简单地使用:

$("li:nth-child(odd) span#symbol").click(function () {
  $(this).closest("li").next("li").toggle();
});

如果您使用多个 id,则存在根本性错误。 id 应该是唯一的。

所以我的建议是,请将它们改为类。通过这种方式,如果你这样做,你可以做类似的事情:

$(function () {
  $(".subtext").hide();
  $("li:nth-child(odd) .symbol").click(function () {
    $(this).closest("li").next("li").toggle();
  });
});
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<div class="tab-content">
  <div id="home" class="tab-pane fade in active ">
    <h3>Product Specs</h3>
    <ul id="questions">
      <li><span class="symbol"><i class="fa fa-gg"></i></span> Do I have to use one of your templates for my vinyl banner?</li>
      <li class="subtext">While we have created templates for you to use as a starting point,  if you don’t want to use them and would like to design your own banner, you have three additional options. First, you can choose the blank template and start designing from scratch on our design tool. Second, you can upload your own files to make a banner. Lastly, you can contact us and have us design something for you.</li>
      <li><span class="symbol"><i class="fa fa-gg"></i></span> Do I have to use one of your templates for my vinyl banner?</li>
      <li class="subtext">While we have created templates for you to use as a starting point,  if you don’t want to use them and would like to design your own banner, you have three additional options. First, you can choose the blank template and start designing from scratch on our design tool. Second, you can upload your own files to make a banner. Lastly, you can contact us and have us design something for you.</li>
      <li><span class="symbol"><i class="fa fa-gg"></i></span> Do I have to use one of your templates for my vinyl banner?</li>
      <li class="subtext">While we have created templates for you to use as a starting point,  if you don’t want to use them and would like to design your own banner, you have three additional options. First, you can choose the blank template and start designing from scratch on our design tool. Second, you can upload your own files to make a banner. Lastly, you can contact us and have us design something for you.</li>
      <li><span class="symbol"><i class="fa fa-gg"></i></span> Do I have to use one of your templates for my vinyl banner?</li>
      <li class="subtext">While we have created templates for you to use as a starting point,  if you don’t want to use them and would like to design your own banner, you have three additional options. First, you can choose the blank template and start designing from scratch on our design tool. Second, you can upload your own files to make a banner. Lastly, you can contact us and have us design something for you.</li>
      <li><span class="symbol"><i class="fa fa-gg"></i></span> Do I have to use one of your templates for my vinyl banner?</li>
      <li class="subtext">While we have created templates for you to use as a starting point,  if you don’t want to use them and would like to design your own banner, you have three additional options. First, you can choose the blank template and start designing from scratch on our design tool. Second, you can upload your own files to make a banner. Lastly, you can contact us and have us design something for you.</li>
      <li><span class="symbol"><i class="fa fa-gg"></i></span> Do I have to use one of your templates for my vinyl banner?</li>
      <li class="subtext">While we have created templates for you to use as a starting point,  if you don’t want to use them and would like to design your own banner, you have three additional options. First, you can choose the blank template and start designing from scratch on our design tool. Second, you can upload your own files to make a banner. Lastly, you can contact us and have us design something for you.</li>
      <li><span class="symbol"><i class="fa fa-gg"></i></span> Do I have to use one of your templates for my vinyl banner?</li>
      <li class="subtext">While we have created templates for you to use as a starting point,  if you don’t want to use them and would like to design your own banner, you have three additional options. First, you can choose the blank template and start designing from scratch on our design tool. Second, you can upload your own files to make a banner. Lastly, you can contact us and have us design something for you.</li>
    </ul>
  </div>
</div>

关于javascript - 我希望一个 <li> 在单击时隐藏另一个 <li> 在同一个 <ul>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33025623/

相关文章:

javascript - 输入类型文本 html5 仅接受 12 个逗号分隔值

PHPMailer 错误 php_network_getaddresses : getaddrinfo failed: Name or service not known

javascript - jQuery 图像更改 - 画廊 View

jquery - Intuit IPP 在 IE 中摧毁 jQuery

javascript - 从元素获取下一个类名

javascript - 在 $.animate 中使用 step() 不使用选择器来停止所有其他动画?

javascript - 获取以编程方式定义的 TitlePane Id 以显示/隐藏

php - 谷歌地方文本搜索 API 没有响应

php - 将 PDO::ATTR_EMULATE_PREPARES 更改为 FALSE 并得到 "Invalid parameter number"错误

javascript - JS Closure Compiler - 不要更改函数名称