WordPress - Onclick功能无效<a> tag

时间:2018-03-19 02:21:11

标签: javascript php html wordpress

I am creating a sliding sidebar menu for my WordPress site using only HTML/CSS/JavaScript. This is from a tutorial from YouTube and followed everything exactly. The issue is that when I click on the buttons, the error reads "Uncaught ReferenceError: openSlideMenu is not defined at HTMLAnchorElement.onclick". I get the same error for the other button. See code below.

HTML (blog.php)

<nav class="navbar">
    <div class="side-menu">
        <span class="open-slide">
            <a href="#" onclick="openSlideMenu()">
               <svg width="30" height="30">
                <path d="M0,5 30,5" stroke="#000" stroke-width="5" />
                <path d="M0,14 30,14" stroke="#000" stroke-width="5" />
                <path d="M0,23 30,23" stroke="#000" stroke-width="5" />
               </svg> 

            </a>
        </span>
    </div>
</nav>

<div id="side-menu" class="side-nav">
    <a  href="#" class="btn-close" onclick="closeSlideMenu()">&times;</a>
        <?php get_sidebar() ?>
</div>



<script type="text/javascript">

    jQuery(document).ready(function(){


    function openSlideMenu() {
        document.getElementById('side-menu').style.width = '250px';
    }

      function closeSlideMenu() {
        document.getElementById('side-menu').style.width = '0';

    }

});

</script>

CSS

.side-nav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top:0;
    left:0;
    background-color: #111;
    opacity: 0.9;
    overflow-x: hidden;
    padding-top: 60px;
    transition: 0.5s;

}

.side-nav a {
    padding: 10px 10px 10px 30px;
    text-decoration: none;
    font-size:22px;
    color: #ccc;
    display: block;
    transition: 0.3s;
}


.side-nav a:hover {
    color: #fff;
}

.side-nav .btn-close {
    position: absolute;
    top: 0;
    right: 22px;
    font-size: 36px;
    margin-left: 50px;
}

The tutorial seemed easy and simple, it should have worked without issues. I wonder could it be something WordPress related? I did all I could.

1 个答案:

答案 0 :(得分:0)

由于您使用的是jQuery,为什么不完全使用它?

有关可能出现问题的更多信息,建议您查看MarsAndBlack's comment

与此同时,我对您的代码进行了一些更改,以便充分利用jQuery。

这里我已经为两个按钮添加了id。 (没必要,但用于方便检测)。

<nav class="navbar">
    <div class="side-menu">
        <span class="open-slide">
            <a href="#" id="openmenu">
               <svg width="30" height="30">
                <path d="M0,5 30,5" stroke="#000" stroke-width="5" />
                <path d="M0,14 30,14" stroke="#000" stroke-width="5" />
                <path d="M0,23 30,23" stroke="#000" stroke-width="5" />
               </svg> 

            </a>
        </span>
    </div>
</nav>

<div id="side-menu" class="side-nav">
    <a  href="#" class="btn-close" id="closemenu">&times;</a>

</div>

还改变了JS使用jQuery来实现click函数而不是vanilla JS,

jQuery(document).ready(function(){

   jQuery('#openmenu').click(function() {
             jQuery('#side-menu').css('width','250px');

   });

   jQuery('#closemenu').click(function() {
             jQuery('#side-menu').css('width','0px');

   });
});

这是一个有效的example