javascript - 我怎样才能只附加一次?

标签 javascript jquery html css

我是 jQuery 新手,请帮我解决这个问题。每次我点击它,链接都会增加。我怎样才能只停止一次附加功能,请帮助我。

$(".dl-trigger").click(function() {
  $("#ChildCat a").each(function() {
    var cat = $(this);
    $("<a />", {
      "href": cat.attr("href"),
      "text": cat.text()
    }).appendTo("#dlmenu");
  });
  $("#dlmenu").slideDown("slow");
});

<强> FIDDLE

最佳答案

使用one() jQuery 中的方法,处理程序每​​个元素执行一次。

$(".dl-trigger").one('click', function() {
  $("#ChildCat a").each(function() {
    var cat = $(this);
    $("<a />", {
      "href": cat.attr("href"),
      "text": cat.text()
    }).appendTo("#dlmenu");
  });
  $("#dlmenu").slideDown("slow");
});
.clear {
  clear: both;
  float: none;
  display: block;
}
.container {
  max-width: 990px;
  width: 100%;
  margin: 0 auto;
}
.inner {
  padding: 0px 10px;
}
#main {
  margin: 10px auto;
}
.grid-two {
  width: 50%;
}
[class*='grid-'] {
  float: left;
  display: block;
  position: relative;
}
#header {
  width: 100%;
  line-height: 60px;
  background: #FFF;
  border-top: 4px solid #33AB83;
  border-bottom: 1px solid #FEFEFE;
  box-shadow: 0px 1px 1px #EEE;
  position: relative;
  left: -5px;
}
#topmenu {
  display: none;
}
span.dl-trigger {
  width: 30px;
  height: 35px;
  line-height: 35px;
  text-align: center;
  background: #33AB83;
  color: #FFF;
  padding: 15px 15px;
  cursor: pointer;
  position: absolute;
  top: -4px;
  right: -15px;
  font-weight: bold;
}
#dlmenu {
  width: 100%;
  height: 100%;
  background: #33AB83;
  padding: 0px 15px;
  position: relative;
  left: -20px;
  top: -15px;
  display: none;
}
#dlmenu a {
  width: 100%;
  display: block;
  padding: 5px 15px;
  margin-left: -15px;
  border-top: 1px solid #2a9471;
  border-bottom: 1px solid #36b88d;
  color: #296d56;
  display: block;
  text-shadow: 1px 1px 0px #49c39a;
}
#dlmenu a:hover {
  background: #3bc094;
}
a {
  text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header id="header">
  <div class="container">
    <div class="inner">
      <div id="logo" class="grid-two">
        <a title="Test Menu" href="http://localhost/">PLEASE CLICK on MENU ICON</a>
      </div>
      <!-- end of #logo -->
      <div id="navigation" class="grid-two">
        <span class="dl-trigger button green">&#9776;</span>
        <ul id="topmenu">
          <li class="ParentCat">
            <span><i class="fa fa-navicon" aria-hidden="true"></i> Parent Category</span>
            <ul id="ChildCat">
              <li><a href="http://localhost/">Category Category A</a>
              </li>
              <li><a href="http://localhost/">Category B</a>
              </li>
              <li><a href="http://localhost/">Category C</a>
              </li>
              <li><a href="http://localhost/">Category D</a>
              </li>
              <li><a href="http://localhost/">Category Category A</a>
              </li>
              <li><a href="http://localhost/">Category B</a>
              </li>
              <li><a href="http://localhost/">Category C</a>
              </li>
              <li><a href="http://localhost/">Category D</a>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <!-- end of #navigation -->
      <div class="clear"></div>
    </div>
    <!-- end of .inner -->
  </div>
  <!-- end of .container -->
</header>
<!-- end of #header -->
<div id="main">
  <div class="wrapper">
    <div class="inner">
      <div id="dlmenu"></div>
    </div>
  </div>
</div>

更新:

如果您只想在单击时切换菜单,则可以使用 slideToggle() 按照@A.Wolff的建议。

$(".dl-trigger").click(function() {
  $("#dlmenu").slideToggle("slow");
});

$("#ChildCat a").appendTo("#dlmenu");
.clear {
  clear: both;
  float: none;
  display: block;
}
.container {
  max-width: 990px;
  width: 100%;
  margin: 0 auto;
}
.inner {
  padding: 0px 10px;
}
#main {
  margin: 10px auto;
}
.grid-two {
  width: 50%;
}
[class*='grid-'] {
  float: left;
  display: block;
  position: relative;
}
#header {
  width: 100%;
  line-height: 60px;
  background: #FFF;
  border-top: 4px solid #33AB83;
  border-bottom: 1px solid #FEFEFE;
  box-shadow: 0px 1px 1px #EEE;
  position: relative;
  left: -5px;
}
#topmenu {
  display: none;
}
span.dl-trigger {
  width: 30px;
  height: 35px;
  line-height: 35px;
  text-align: center;
  background: #33AB83;
  color: #FFF;
  padding: 15px 15px;
  cursor: pointer;
  position: absolute;
  top: -4px;
  right: -15px;
  font-weight: bold;
}
#dlmenu {
  width: 100%;
  height: 100%;
  background: #33AB83;
  padding: 0px 15px;
  position: relative;
  left: -20px;
  top: -15px;
  display: none;
}
#dlmenu a {
  width: 100%;
  display: block;
  padding: 5px 15px;
  margin-left: -15px;
  border-top: 1px solid #2a9471;
  border-bottom: 1px solid #36b88d;
  color: #296d56;
  display: block;
  text-shadow: 1px 1px 0px #49c39a;
}
#dlmenu a:hover {
  background: #3bc094;
}
a {
  text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header id="header">
  <div class="container">
    <div class="inner">
      <div id="logo" class="grid-two">
        <a title="Test Menu" href="http://localhost/">PLEASE CLICK on MENU ICON</a>
      </div>
      <!-- end of #logo -->
      <div id="navigation" class="grid-two">
        <span class="dl-trigger button green">&#9776;</span>
        <ul id="topmenu">
          <li class="ParentCat">
            <span><i class="fa fa-navicon" aria-hidden="true"></i> Parent Category</span>
            <ul id="ChildCat">
              <li><a href="http://localhost/">Category Category A</a>
              </li>
              <li><a href="http://localhost/">Category B</a>
              </li>
              <li><a href="http://localhost/">Category C</a>
              </li>
              <li><a href="http://localhost/">Category D</a>
              </li>
              <li><a href="http://localhost/">Category Category A</a>
              </li>
              <li><a href="http://localhost/">Category B</a>
              </li>
              <li><a href="http://localhost/">Category C</a>
              </li>
              <li><a href="http://localhost/">Category D</a>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <!-- end of #navigation -->
      <div class="clear"></div>
    </div>
    <!-- end of .inner -->
  </div>
  <!-- end of .container -->
</header>
<!-- end of #header -->
<div id="main">
  <div class="wrapper">
    <div class="inner">
      <div id="dlmenu"></div>
    </div>
  </div>
</div>

关于javascript - 我怎样才能只附加一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37587393/

相关文章:

html - 通过 tcp 和浏览器发送带有 HTTP 协议(protocol)的 html 文件显示错误

javascript - Nodejs Nightmare 库按钮单击

java - 语法错误: missing ) after argument list while reading csv file and alerting in js

javascript - 如何在javascript中包含html

javascript - 使用动态创建的按钮调用 SignalR hub 方法

html - Bootstrap 弹出窗口太窄

javascript - Angularjs ng-repeat 和子元素

javascript - 如何对包含日期和值的数组进行排序?

javascript - Angular JS 中的条件 ng-repeat

javascript - 如何使用 javascript 删除我的选项?