这不打击选择器

时间:2016-07-17 07:13:06

标签: javascript jquery

这是我的js。 - .minidiv没有隐藏和显示

var accordion_head = $('.global_right_wrapper .stickySection-a .sticky-close');
accordion_head.on('click', function (event) {
var $a = $(this);
event.preventDefault();

if ($a.hasClass('active')) {
  $(".minidiv", $a).show(); // this is not working****
    $a.removeClass('active').siblings('.content').slideUp();

}

else {
  $(".minidiv", $a).hide(); // this is not working****
    $a.addClass('active').siblings('.content').slideDown();

} });

这是我的HTML。 - .minidiv没有隐藏和显示

    <div id="stickyFooter-a" class="global_right_wrapper">
    <div class="stickySection-a">
    <h2>WHAT IS JANUVIA®?</h2><span class="sticky-close"><h2>[open]</h2></span>
    <div class="minidiv">JANUVIA (jah-NEW-vee-ah) is a once-daily prescription pill that, along with diet and exercise, 
          <span class="desktopContent">helps lower blood sugar levels in adults with type 2 diabetes.</span>
    </div>
        <div class="content">
             <P>JANUVIA (jah-NEW-vee-ah) is a once-daily prescription pill that, along with diet and exercise, helps lower blood sugar levels in adults with type 2 diabetes.</P>
          <P>JANUVIA should not be used in patients with type 1 diabetes or with diabetic ketoacidosis (increased ketones in the blood or urine). If you have had pancreatitis (inflammation of the pancreas), it is not known if you have a higher chance of getting it while taking JANUVIA.</P>
        </div>
    </div>


   <div class="stickySection-a">
    <h2>IMPORTANT SAFETY INFORMATION</h2><span class="sticky-close"><h2>[open]</h2></span>
        <div class="minidiv">Serious side effects can happen in people who take JANUVIA, including pancreatitis, which may 
          <span class="desktopContent">be severe and lead to death. Before you start taking JANUVIA, tell your doctor if you've ever had pancreatitis. Stop taking JANUVIA and call your doctor right</span>
        </div>
      <div class="content">
        <div class="mobile-only">
          <p>Serious side effects can happen in people who take JANUVIA, including pancreatitis, which may be severe and lead to death. Before you start taking JANUVIA, tell your doctor if you've ever had pancreatitis. Stop taking JANUVIA and call your doctor right away if you have pain in your stomach area (abdomen) that is severe and will not go away. The pain may be felt going from your abdomen through to your back. The pain may happen with or without vomiting. These may be symptoms of pancreatitis.</p>
          <p>Do not take JANUVIA if you are allergic to any of its ingredients, including sitagliptin. Symptoms of serious allergic reactions to JANUVIA, including rash, hives, and swelling of the face, lips, tongue, and throat that may cause difficulty breathing or swallowing, can occur. If you have any symptoms of a serious allergic reaction, stop taking JANUVIA and call your doctor right away.</p>
        </div>
        <div class="desktop-only">
        <p>Serious side effects can happen in people who take JANUVIA, including pancreatitis, which may be severe and lead to death. Before you start taking JANUVIA, tell your doctor if you've ever had pancreatitis. Stop taking JANUVIA and call your doctor right away if you have pain in your stomach area (abdomen) that is severe and will not go away. The pain may be felt going from your abdomen through to your back. The pain may happen with or without vomiting. These may be symptoms of pancreatitis.</p>
        <p>Do not take JANUVIA if you are allergic to any of its ingredients, including sitagliptin. Symptoms of serious allergic reactions to JANUVIA, including rash, hives, and swelling of the face, lips, tongue, and throat that may cause difficulty breathing or swallowing, can occur. If you have any symptoms of a serious allergic reaction, stop taking JANUVIA and call your doctor right away.</p>
        <p>Kidney problems, sometimes requiring dialysis, have been reported.</p>
        <p>Some people who take medicines called DPP-4 inhibitors like JANUVIA, may develop joint pain that can be severe. Call your doctor if you have severe joint pain.</p>
        <p>If you take JANUVIA with another medicine that can cause low blood sugar (hypoglycemia), such as a sulfonylurea or insulin, your risk of getting low blood sugar is higher. The dose of your sulfonylurea medicine or insulin may need to be lowered while you use JANUVIA. Signs and symptoms of low blood sugar may include headache, drowsiness, weakness, dizziness, confusion, irritability, hunger, fast heart beat, sweating, and feeling jittery.</p>
        <p>Your doctor may do blood tests before and during treatment with JANUVIA to see how well your kidneys are working. Based on these results, your doctor may change your dose of JANUVIA. The most common side effects of JANUVIA are upper respiratory tract infection, stuffy or runny nose and sore throat, and headache.</p>
        <p>You are encouraged to report negative side effects of prescription drugs to the FDA. Visit  <a href="http://www.fda.gov/medwatch">http://www.fda.gov/medwatch</a> or call 1-800-FDA-1088.</p>
        <p class="sect-para">Please read the accompanying <a href="http://doctor.merck.com/product/usa/pi_circulars/j/januvia/januvia_mg.pdf" target="_blank">Medication Guide</a> for JANUVIA® (sitagliptin) tablets and discuss it with your doctor. The physician <a href="http://doctor.januvia.com/sitagliptin/januvia/consumer/prescribing-information.xhtml" target="_blank">Prescribing Information</a> also is available.</p>
        </div>
        <p class="sect-para1">Having trouble paying for your Merck medicine? Merck may be able to help.  Visit <a href="http://www.merckhelps.com">merckhelps.com </a></p>
      </div>
    </div>
</div>

'this'运营商与其他人合作,但不是在我隐藏并显示div时。 我在js代码中添加了注释

  

“//这不起作用”

2 个答案:

答案 0 :(得分:0)

要达到预期效果,请使用以下选项

if ($a.hasClass('active')) {
  $(".minidiv").hide(); // this is not working****
    $a.removeClass('active').siblings('.content').slideUp();

}

else {
  $(".minidiv").show(); // this is not working****
    $a.addClass('active').siblings('.content').slideDown();

} });

http://codepen.io/nagasai/pen/akEZGa

答案 1 :(得分:0)

你的$a即。 $(this)通常是<span class="sticky-close"><h2>[open]</h2></span>,而$(".minidiv", $a)正在.minidiv内搜索<span class="sticky-close"><h2>[open]</h2></span> $(".minidiv", $a).show(); 内部$(".minidiv", $a.parent()).show(); ,它将不会回馈任何内容。

替换

var accordion_head = $('.global_right_wrapper .stickySection-a .sticky-close');
accordion_head.on('click', function(event) {
  var $a = $(this);
  event.preventDefault();

  if ($a.hasClass('active')) {
    $(".minidiv", $a.parent()).show(); // this is not working****
    $a.removeClass('active').siblings('.content').slideUp();

  } else {
    $(".minidiv", $a.parent()).hide(); // this is not working****
    $a.addClass('active').siblings('.content').slideDown();

  }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="stickyFooter-a" class="global_right_wrapper">
  <div class="stickySection-a">
    <h2>WHAT IS JANUVIA®?</h2><span class="sticky-close"><h2>[open]</h2></span>
    <div class="minidiv">JANUVIA (jah-NEW-vee-ah) is a once-daily prescription pill that, along with diet and exercise,
      <span class="desktopContent">helps lower blood sugar levels in adults with type 2 diabetes.</span>
    </div>
    <div class="content">
      <P>JANUVIA (jah-NEW-vee-ah) is a once-daily prescription pill that, along with diet and exercise, helps lower blood sugar levels in adults with type 2 diabetes.</P>
      <P>JANUVIA should not be used in patients with type 1 diabetes or with diabetic ketoacidosis (increased ketones in the blood or urine). If you have had pancreatitis (inflammation of the pancreas), it is not known if you have a higher chance of getting
        it while taking JANUVIA.</P>
    </div>
  </div>


  <div class="stickySection-a">
    <h2>IMPORTANT SAFETY INFORMATION</h2><span class="sticky-close"><h2>[open]</h2></span>
    <div class="minidiv">Serious side effects can happen in people who take JANUVIA, including pancreatitis, which may
      <span class="desktopContent">be severe and lead to death. Before you start taking JANUVIA, tell your doctor if you've ever had pancreatitis. Stop taking JANUVIA and call your doctor right</span>
    </div>
    <div class="content">
      <div class="mobile-only">
        <p>Serious side effects can happen in people who take JANUVIA, including pancreatitis, which may be severe and lead to death. Before you start taking JANUVIA, tell your doctor if you've ever had pancreatitis. Stop taking JANUVIA and call your doctor
          right away if you have pain in your stomach area (abdomen) that is severe and will not go away. The pain may be felt going from your abdomen through to your back. The pain may happen with or without vomiting. These may be symptoms of pancreatitis.</p>
        <p>Do not take JANUVIA if you are allergic to any of its ingredients, including sitagliptin. Symptoms of serious allergic reactions to JANUVIA, including rash, hives, and swelling of the face, lips, tongue, and throat that may cause difficulty breathing
          or swallowing, can occur. If you have any symptoms of a serious allergic reaction, stop taking JANUVIA and call your doctor right away.</p>
      </div>
      <div class="desktop-only">
        <p>Serious side effects can happen in people who take JANUVIA, including pancreatitis, which may be severe and lead to death. Before you start taking JANUVIA, tell your doctor if you've ever had pancreatitis. Stop taking JANUVIA and call your doctor
          right away if you have pain in your stomach area (abdomen) that is severe and will not go away. The pain may be felt going from your abdomen through to your back. The pain may happen with or without vomiting. These may be symptoms of pancreatitis.</p>
        <p>Do not take JANUVIA if you are allergic to any of its ingredients, including sitagliptin. Symptoms of serious allergic reactions to JANUVIA, including rash, hives, and swelling of the face, lips, tongue, and throat that may cause difficulty breathing
          or swallowing, can occur. If you have any symptoms of a serious allergic reaction, stop taking JANUVIA and call your doctor right away.</p>
        <p>Kidney problems, sometimes requiring dialysis, have been reported.</p>
        <p>Some people who take medicines called DPP-4 inhibitors like JANUVIA, may develop joint pain that can be severe. Call your doctor if you have severe joint pain.</p>
        <p>If you take JANUVIA with another medicine that can cause low blood sugar (hypoglycemia), such as a sulfonylurea or insulin, your risk of getting low blood sugar is higher. The dose of your sulfonylurea medicine or insulin may need to be lowered
          while you use JANUVIA. Signs and symptoms of low blood sugar may include headache, drowsiness, weakness, dizziness, confusion, irritability, hunger, fast heart beat, sweating, and feeling jittery.</p>
        <p>Your doctor may do blood tests before and during treatment with JANUVIA to see how well your kidneys are working. Based on these results, your doctor may change your dose of JANUVIA. The most common side effects of JANUVIA are upper respiratory
          tract infection, stuffy or runny nose and sore throat, and headache.</p>
        <p>You are encouraged to report negative side effects of prescription drugs to the FDA. Visit <a href="http://www.fda.gov/medwatch">http://www.fda.gov/medwatch</a> or call 1-800-FDA-1088.</p>
        <p class="sect-para">Please read the accompanying <a href="http://doctor.merck.com/product/usa/pi_circulars/j/januvia/januvia_mg.pdf" target="_blank">Medication Guide</a> for JANUVIA® (sitagliptin) tablets and discuss it with your doctor. The physician <a href="http://doctor.januvia.com/sitagliptin/januvia/consumer/prescribing-information.xhtml"
          target="_blank">Prescribing Information</a> also is available.</p>
      </div>
      <p class="sect-para1">Having trouble paying for your Merck medicine? Merck may be able to help. Visit <a href="http://www.merckhelps.com">merckhelps.com </a>
      </p>
    </div>
  </div>
</div>

&#13;
&#13;
[{
    "$order": 2998,
    "text": "Rosales Glenn",
    "id": 375
}, {
    "$order": 2999,
    "text": "Dillard Joyce",
    "id": 450
}, {
    "$order": 3000,
    "text": "Maryellen Hogan",
    "id": 365
}, {
    "$order": 3002,
    "text": "Jeannette Church",
    "id": 207
}]
&#13;
{
    "$order": 3001,
    "text": "Jeannette Chichi",
    "id": 205
}
&#13;
&#13;
&#13;