【问题标题】:Onclick or href not working on Dropdown menu on hover effectOnclick 或 href 在悬停效果的下拉菜单上不起作用
【发布时间】:2017-10-04 11:09:31
【问题描述】:

我有一个悬停时带有下拉菜单的菜单,单击它应该会打开一个新页面。 这是 HTML;

<ul class=“Mainmenu” id=“Mainmenu”>
<li class=“dropdown”><a onclick=“window.location.reload()”>Home</a></li>
<li class=“dropdown”><a href=“Page1.html” onclick=“Loadpage(); return false;”>About us</a>
<ul class=“dropdown-menu” id=“menu1”>
<li class=“dropdownlink” id=“link1”><a href=“#”>Domestic</a></li>
<li class=“dropdownlink” id=“link2”><a href=“#”>International</a></li>
</ul>
</li>
<li class=“dropdown”><a onclick=“Loadteampage(); return false;”>Team</a></li>
</ul>

这里是 JavaScript:

function Loadpage(){
window.location.href=“Page1.html”;
}
function Loadteampage(){
window.location.href=“Team.html”;
}

这里是 CSS

.Mainmenu{ 
height: 15px; 
font-family: Calibri;
font-size 12. 5px; 
display: inline; 
position: absolute; 
width: 100%;
line-height: 15px; 
text-align: center; 
list-style: none; 
top 18px;
}

.Mainmenu a{
float: left;
font-size: 14px; 
color: #FAF6AF; 
text-align: center; 
padding: 5px 10px; 
text-decoration: none; 
height: 15px; 
line -height: 15px; 
transition: all 0.7s ease;
}

ul.Mainmenu > li{
float: left; 
position: relative;
}

ul.Mainmenu li > a{
height: 15px; 
line-height: 15px; 
text-align: Center; 
display: block; 
background: #000000; 
color: #FAF6AF; 
white-space: nowrap; 
}

ul.Mainmenu li:hover > a{
background-color: #FAF6AF; 
color #000000; 
cursor: pointer;}

ul.Mainmenu > li ul {
display: none; 
position: absolute;
}

ul.Mainmenu > li:hover ul{
display: block; 
cursor: pointer;
}

ul.Mainmenu > li ul li{
display: block; 
position: relative; 
top: 25px; 
left: -40px;
}

.dropdownlink a{
text-align: center; 
line-height: 15px; 
font-size: 12.5px; 
padding: 0px 0px 0px 0px; 
margin: 0; 
width: 100%; 
}

“关于我们”在悬停时应充当下拉菜单,并在单击时充当链接。但是“关于我们”上的 onclick 和 href 不起作用。即使我尝试过 alert();在 onclick 但它不起作用。

请在下面找到 JSFIDDLE 链接:

JSFIDDLE

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 为什么要使用 javascript 重定向,而不是简单地使用 &lt;a href="..." 属性?
  • 保持简单:删除 javascript,并专注于 href。除非您的标记格式错误,否则 href 将起作用。但是您包含的标记没有格式错误,所以我怀疑这里还有其他东西在起作用。记住 - 当你遇到麻烦时,simplify - 这就是你最终找到解决你自己问题的方法,或者minimal complete and verifiable example
  • @cale_b href 是我的第一选择,但我猜这是 css 的问题,因为这只发生在带有下拉菜单的菜单项上。其他菜单项的其他 href/onclick 功能工作正常!

标签: javascript css html dropdown


【解决方案1】:

编辑以包含我最喜欢的扩展列表 javascript

/*

CollapsibleLists.js

An object allowing lists to dynamically expand and collapse

Created by Stephen Morley - http://code.stephenmorley.org/ - and released under
the terms of the CC0 1.0 Universal legal code:

http://creativecommons.org/publicdomain/zero/1.0/legalcode

*/

// create the CollapsibleLists object
var CollapsibleLists =
    new function(){

      /* Makes all lists with the class 'collapsibleList' collapsible. The
       * parameter is:
       *
       * doNotRecurse - true if sub-lists should not be made collapsible
       */
      this.apply = function(doNotRecurse){

        // loop over the unordered lists
        var uls = document.getElementsByTagName('ul');
        for (var index = 0; index < uls.length; index ++){

          // check whether this list should be made collapsible
          if (uls[index].className.match(/(^| )collapsibleList( |$)/)){

            // make this list collapsible
            this.applyTo(uls[index], true);

            // check whether sub-lists should also be made collapsible
            if (!doNotRecurse){

              // add the collapsibleList class to the sub-lists
              var subUls = uls[index].getElementsByTagName('ul');
              for (var subIndex = 0; subIndex < subUls.length; subIndex ++){
                subUls[subIndex].className += ' collapsibleList';
              }

            }

          }

        }

      };

      /* Makes the specified list collapsible. The parameters are:
       *
       * node         - the list element
       * doNotRecurse - true if sub-lists should not be made collapsible
       */
      this.applyTo = function(node, doNotRecurse){

        // loop over the list items within this node
        var lis = node.getElementsByTagName('li');
        for (var index = 0; index < lis.length; index ++){

          // check whether this list item should be collapsible
          if (!doNotRecurse || node == lis[index].parentNode){

            // prevent text from being selected unintentionally
            if (lis[index].addEventListener){
              lis[index].addEventListener(
                  'mousedown', function (e){ e.preventDefault(); }, false);
            }else{
              lis[index].attachEvent(
                  'onselectstart', function(){ event.returnValue = false; });
            }

            // add the click listener
            if (lis[index].addEventListener){
              lis[index].addEventListener(
                  'click', createClickListener(lis[index]), false);
            }else{
              lis[index].attachEvent(
                  'onclick', createClickListener(lis[index]));
            }

            // close the unordered lists within this list item
            toggle(lis[index]);

          }

        }

      };

      /* Returns a function that toggles the display status of any unordered
       * list elements within the specified node. The parameter is:
       *
       * node - the node containing the unordered list elements
       */
      function createClickListener(node){

        // return the function
        return function(e){

          // ensure the event object is defined
          if (!e) e = window.event;

          // find the list item containing the target of the event
          var li = (e.target ? e.target : e.srcElement);
          while (li.nodeName != 'LI') li = li.parentNode;

          // toggle the state of the node if it was the target of the event
          if (li == node) toggle(node);

        };

      }

      /* Opens or closes the unordered list elements directly within the
       * specified node. The parameter is:
       *
       * node - the node containing the unordered list elements
       */
      function toggle(node){

        // determine whether to open or close the unordered lists
        var open = node.className.match(/(^| )collapsibleListClosed( |$)/);

        // loop over the unordered list elements with the node
        var uls = node.getElementsByTagName('ul');
        for (var index = 0; index < uls.length; index ++){

          // find the parent list item of this unordered list
          var li = uls[index];
          while (li.nodeName != 'LI') li = li.parentNode;

          // style the unordered list if it is directly within this node
          if (li == node) uls[index].style.display = (open ? 'block' : 'none');

        }

        // remove the current class from the node
        node.className =
            node.className.replace(
                /(^| )collapsibleList(Open|Closed)( |$)/, '');

        // if the node contains unordered lists, set its class
        if (uls.length > 0){
          node.className += ' collapsibleList' + (open ? 'Open' : 'Closed');
        }

      }

    }();

推荐的Html是

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <title>Vakram Mohan Help</title>
   <link rel="stylesheet" href="reset.css" />

    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:regular,bold,italic,thin,light,bolditalic,black,medium&amp;lang=en">       

   <script type="text/javascript" src="CollapsibleLists.js"></script>

</head>
<body>
<div>
<UL id="Mainmenu">
    <li>
        <h2><span>Menu</span></h2>
        <ul>
            <li> <span><a onclick=“window.location.reload()”>Home</a></span></li>

            <li><span><a href="#" onclick="return btntest_onclick()">About us</a></span>
                <ul>
                    <li><span><a href=“#”>Domestic</a></span></li>
                    <li><span><a href=“#”>International</a></span></li>
                </ul>
            </li>



        </ul>
    </li><!--Menu end -->
</UL> <!-- Main Menu end -->

</body>
<script language="javascript" type="text/javascript">
function btntest_onclick() 
{
    window.location.href = "http://www.google.com";
}
</script>
<script>
    CollapsibleLists.applyTo(document.getElementById('Mainmenu'));

</script>
</head>
</html>

请注意,我没有使用您的 CSS 文件。 真是一团糟

#para1 {
    text-align: center;
    color: red;
}

这是一个 ID 选择器

.center {
    text-align: center;
    color: red;
}

这是一个类选择器。

Reset.css 在这里

html, body, header, footer, hgroup, nav, main, article, section, figure, figcaption, h1, h2, h3, ul, li, body, div, p, img
{
   margin: 0;
   padding: 0;
   font-size: 100%;
   vertical-align: baseline;
   border: 0;
}

【讨论】:

  • 附带说明.. 我认为您不应该让 Class= Mainmenu 与 id 同名
  • ID和类名到目前为止没有产生任何问题,我正确地得到了css效果。但我无法在您的代码中得到结果,链接有效,但 CSS 效果无效。你能把reset.css和mainmenu.css给我吗?
  • 这是一个实时站点上可扩展列表的示例。jeremiahstillings.net/naroggsplace/Solstice.html
  • 如果我的解决方案不是你想要的,试试这个资源w3schools.com/howto/howto_css_dropdown.asp
  • 是的卡伦巴。为什么这么多代码,这么少的解释,来解决应该相当容易解决的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多