在JavaScript中单击事件触发器之前首先触发模糊事件

时间:2015-06-25 11:28:45

标签: javascript jquery onclick onblur

我正在尝试在模糊事件中展开面板并在单击时调用其他一些代码。但是没有触发点击事件,只触发模糊事件触发。

要重现,请转到小提琴并在输入框中键入一些文本,然后单击“测试”按钮。

HTML:

<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div id="accordion">
  <h3>Section 1</h3>

  <div>
      <input type="text" />
    <p>
    Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
    ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
    amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
    odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
    </p>
  </div>
  <h3>Section 2</h3>
  <div>
    <p>
    Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
    purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
    velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
    suscipit faucibus urna.
    </p>
  </div>
  <h3>Section 3</h3>
  <div>
    <p>
    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
    </p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
  <h3>Section 4</h3>
  <div>
    <p>
    Cras dictum. Pellentesque habitant morbi tristique senectus et netus
    et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
    faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
    mauris vel est.
    </p>
    <p>
    Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
    inceptos himenaeos.
    </p>
  </div>
</div>

<input type="button" value="test" id="test" />

JavaScript的:

  $(function() {

      $( "#accordion" ).accordion();

      $("#test").on("click", function() {
          alert("Click event triggered");
      });

      $("input").on("blur", function() {
          $('#accordion .ui-accordion-content').show();
      });

  });

您可以在此处查看JSFiddle:http://jsfiddle.net/ranjitsachin/zbrntv2r/3/

注意:我发现了一些非标准方式的备用解决方案,例如我们可以使用onmousedown而不是click事件。

另外,我不想在点击事件中调用模糊事件。

1 个答案:

答案 0 :(得分:0)

在点击事件之前添加此项,以阻止模糊事件发生。

<?php
$string = "somehting<br><br>";
$string = htmlspecialchars(str_replace("<br>","<c>",$string));

var_dump(strpos($string, htmlspecialchars("<c>"))); // int(9)