简体   繁体   English

如何在HTML div中使用JavaScript OnBlur()?

[英]How can I use JavaScript OnBlur() in HTML div?

I have a menu and I can open and close the menu clicking on a tag. 我有一个菜单,单击标签可以打开和关闭菜单。 It works. 有用。 Also I use OnBlur(). 我也使用OnBlur()。 Actually it works too but When I click a div in the menu, OnBlur active, menu closes and div's onclick doesn't works. 其实它也可以,但是当我单击菜单中的div时,OnBlur处于活动状态,菜单关闭并且div的onclick无效。 How can I fix this problem ? 我该如何解决这个问题?

In JavaScript Codes; 在JavaScript代码中;

<script>
  function Acc_Show() {
    var q = document.getElementById("he-my");
    q.style.display = "";
    document.getElementById("my-account-btn").focus();
    document.getElementById("my-account-btn").setAttribute("onclick", "Acc_Hide()");
  }
  function Acc_Hide() {
    var q = document.getElementById("he-my");
    q.style.display = "none";
    document.getElementById("my-account-btn").setAttribute("onclick", "Acc_Show()");
  }
</script>

In HTML codes; 在HTML代码中;

<a id="my-account-btn" href="javascript:;" onclick="Acc_Show()" onblur="Acc_Hide()">My Account</a>

My Menu's Image; 我的菜单的图像;

http://i.stack.imgur.com/OMg6n.png http://i.stack.imgur.com/OMg6n.png

UPDATES Additional Codes From OP: 从OP 更新其他代码:

<div class="he-myac" id="he-my" style="display:none;">
  <div id="my-account-wrapper">
    <div class="myaccount" onclick="GoAdress('test.aspx')">Test</div>  
    <br/>
    <div class="myaccount" onclick="GoAdress('settings.aspx')" >Settings</div>
    <br/>
    <div class="myaccount" onclick="GoAdress('log_out.aspx')">Log Out</div>
  </div>

I have a feeling this line q.style.display = ""; 我感觉这行q.style.display = ""; in your Acc_Show() function is causing the issue. 您的Acc_Show()函数中引起此问题。 Try replace it with q.style.display = "block"; 尝试将其替换为q.style.display = "block"; .

When an onblur event is triggered, your Acc_Hide() function hides your <div> by calling q.style.display = "none"; 当触发onblur事件时,您的Acc_Hide()函数通过调用q.style.display = "none";隐藏<div> q.style.display = "none"; . Then the next click on my-account-btn triggers your Acc_Show() function, which set your q.style.display to an empty string. 然后,单击my-account-btn会触发您的Acc_Show()函数,该函数q.style.display您的q.style.display设置为空字符串。 That doesn't unhide your <div> block. 这不会取消隐藏您的<div>块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM