【问题标题】:How to remove a class from a link... on only one page?如何从链接中删除类...仅在一页上?
【发布时间】:2013-06-13 19:17:52
【问题描述】:

我正在尝试创建一个 404.php 页面,并想从链接中删除“当前状态”类,仅在该页面上。

到目前为止,这就是我所拥有的......

jQuery( function() {

    if ( TEMPLATE_URI + '/404.php'.hasClass( '.current_page_parent' ) ) {

            jQuery( '.menu' ).removeClass( '.current_page_parent' );
    }

});

这似乎不起作用。

我是 jQuery 主题的初学者,因此我们将不胜感激!

【问题讨论】:

    标签: php jquery wordpress


    【解决方案1】:

    我认为这应该可行:

    if(window.location.href.indexOf('404.php') != -1){
         jQuery('.current_page_parent').removeClass('current_page_parent')
    }
    

    另外,当使用removeClass时,你不需要放点。

    【讨论】:

    • 这是一个疯狂的猜测,因为在他的问题中,他(我认为)正在搜索字符串 '404.php'
    【解决方案2】:

    WordPress automatically uses the file 404.php to show 404 error pages. 这样您就不必在代码中检查 404.php。

    只要把这段Javascript代码放在文件404.php,preferrably at the bottom

    <script type="text/javascript">
    jQuery(document).ready(function($) {
        jQuery('.menu').removeClass('current_page_parent');
    });
    </script>
    

    否则,您可以在任何模板 (PHP) 文件中使用函数is_404() 来查看当前页面是否为 404 错误页面。像这样的:

    <?php if (is_404()): ?>
    jQuery('.menu').removeClass('current_page_parent');
    <?php endif; ?>
    

    【讨论】:

    • 它仍然无法工作!脚本什么也不做,php 打印在屏幕上。这是一个链接,可以向您展示我正在做的事情...link
    【解决方案3】:

    感谢@Christian Daven 和@Karl-Andre Gagnon 的帮助!基本上我所做的是将两个答案的部分结合起来并得到了这个(我已将其放置在我的 404 模板页面中)...

    <script type="text/javascript">
    
        jQuery(document).ready(function($) {
    
            jQuery( '.current_page_parent' ).removeClass( 'current_page_parent' );
    
        });
    
    </script> 
    

    【讨论】:

    • 说你可以玩 404 模板对你的问题来说是一个很好的补充^^,在你的下一个问题中添加所有有用的信息!
    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多