单击li标签中的href时,链接未打开

时间:2020-06-20 00:59:18

标签: javascript html css

我正在使用https://codepen.io/ryanmorr/pen/LVzYmx/小麻烦但大问题

当我单击链接a href时,它不会打开,这些代码似乎正确吗?

所以我要插入所有的CSS,JS-这些可能触发此无法打开的链接。但是我认为只使用href应该就可以了吗?

我不确定这是仅在我身上还是在其他任何人身上?

[].slice.call(document.querySelectorAll('.dropdown .nav-link')).forEach(function(el){
    el.addEventListener('click', onClick, false);
});

function onClick(e){
    e.preventDefault();
    var el = this.parentNode;
    el.classList.contains('show-submenu') ? hideSubMenu(el) : showSubMenu(el);
}

function showSubMenu(el){
    el.classList.add('show-submenu');
    document.addEventListener('click', function onDocClick(e){
        e.preventDefault();
        if(el.contains(e.target)){
            return;
        }
        document.removeEventListener('click', onDocClick);
        hideSubMenu(el);
    });
}

function hideSubMenu(el){
    el.classList.remove('show-submenu');
}
/* Page */

html {
    box-sizing: border-box;
}

*, 
*:before, 
*:after {
    box-sizing: inherit;
}

body {
    background-color: #3498db; 
}

.nav {
    width: 320px;
    margin: 0 auto 0 auto; 
    text-align: center;
}

/* Navigation */

.nav {
    font-family: Georgia, Arial, sans-serif;
    font-size: 16px;
}

.nav-items {
    padding: 0;
    list-style: none;
}

.nav-item {
    display: inline-block;
    margin-right: 8px;
}

.nav-item:last-child {
    margin-right: 0;
}

.nav-link,
.nav-link:link, 
.nav-link:visited, 
.nav-link:active,
.submenu-link,
.submenu-link:link, 
.submenu-link:visited, 
.submenu-link:active {
    display: block;
    position: relative;
    font-size: 14px;
    letter-spacing: 1px;
    cursor: pointer;
    text-decoration: none;
    outline: none;
}

.nav-link,
.nav-link:link, 
.nav-link:visited, 
.nav-link:active {
    color: #fff;
    font-weight: bold;
}

.nav-link::before {
    content: "";
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    height: 3px;
    background: rgba(0,0,0,0.2);
    opacity: 0;
    -webkit-transform: translate(0, 10px);
    transform: translate(0, 10px);
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.nav-link:hover::before,
.nav-link:hover::before {
    opacity: 1;
    -webkit-transform: translate(0, 5px);
    transform: translate(0, 5px);
}

.dropdown {
    position: relative;
}

.dropdown .nav-link {
    padding-right: 15px;
    height: 17px;
    line-height: 17px;
}

.dropdown .nav-link::after {
    content: "";
    position:absolute;
    top: 6px;
    right: 0;
    border: 5px solid transparent;
    border-top-color: #fff;
}

.submenu {
    position: absolute;
    top: 100%;
    left: 50%;
    z-index: 100;
    width: 200px;
    margin-left: -100px;
    background: #fff;
    border-radius: 3px;
    line-height: 1.46667;
    margin-top: -5px;
    box-shadow: 0 0 8px rgba(0,0,0,.3);
    opacity:0;
    -webkit-transform: translate(0, 0) scale(.85);
    transform: translate(0, 0)scale(.85);
    transition: transform 0.1s ease-out, opacity 0.1s ease-out;
    pointer-events: none;
}

.submenu::after, 
.submenu::before {
    content: ""; 
    position: absolute;
    bottom: 100%;
    left: 50%;
    margin-left: -10px;
    border: 10px solid transparent;
    height: 0;
}

.submenu::after {
    border-bottom-color: #fff;
}

.submenu::before {
    margin-left: -13px;
    border: 13px solid transparent;
    border-bottom-color: rgba(0,0,0,.1);
    -webkit-filter:blur(1px);
    filter:blur(1px);
}

.submenu-items {
    list-style: none;
    padding: 10px 0;
}

.submenu-item {
    display: block;
    text-align: left;
}

.submenu-link,
.submenu-link:link, 
.submenu-link:visited, 
.submenu-link:active {
    color: #3498db;
    padding: 10px 20px;
}

.submenu-link:hover {
    text-decoration: underline;
}

.submenu-seperator {
    height: 0;
    margin: 12px 10px;
    border-top: 1px solid #eee;
}

.show-submenu .submenu {
    opacity: 1;
    -webkit-transform: translate(0, 25px) scale(1);
    transform: translate(0, 25px) scale(1);
    pointer-events: auto;
}
<nav class="nav" id="header">
    <ul class="nav-items">
        <li class="nav-item dropdown">
            <a class="nav-link"><span>Link</span></a>
            <nav class="submenu">
                <ul class="submenu-items">
                    <li class="submenu-item"><a href="https://www.facebook.com/" target="_blank" class="submenu-link">Facebook</a></li>
                    <li class="submenu-item"><a href="https://codepen.io/ryanmorr/pen/LVzYmx/" target="_blank" class="submenu-link">This HTML</a></li>
                    <li class="submenu-item"><hr class="submenu-seperator" /></li>
                    <li class="submenu-item"><a href="https://www.google.com/" target="_blank" class="submenu-link">Google</a></li>
                </ul>
            </nav>
        </li>
        <li class="nav-item dropdown">
            <a class="nav-link"><span>Info</span></a>
            <nav class="submenu">
                <ul class="submenu-items">
                    <li class="submenu-item"><a class="submenu-link">???</a></li>
                </ul>
            </nav>
        </li>
    </ul>
</nav>

提前谢谢

1 个答案:

答案 0 :(得分:3)

似乎e.preventDefault();引起了问题。请从以下位置将其删除:

function showSubMenu(el){
    el.classList.add('show-submenu');
    document.addEventListener('click', function onDocClick(e){
        e.preventDefault(); //this line
        if(el.contains(e.target)){
            return;
        }
        document.removeEventListener('click', onDocClick);
        hideSubMenu(el);
    });
}

它现在应该按预期打开链接。

说明:

看看here。您会看到添加e.preventDefault();将会继续传播,直到停止为止。

因此,如果您防止在父元素上使用默认的event,则该设置也会传播到所有子元素。这意味着href的默认行为也将得到防止。