【问题标题】:Show Div if URL is exactly如果 URL 完全正确,则显示 Div
【发布时间】:2017-09-13 07:31:26
【问题描述】:

我想找出显示 div ID 的 javascript(我也可以使用 jquery)(#main,我在 css 中将其设置为显示:无;)如果 URL 完全类似于:

http://test.testdomain.com/news

但不是这样的

http://test.testdomain.com/news/34
http://test.testdomain.com/news/96
http://test.testdomain.com/news/341254

有什么想法吗?

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    我相信这是你需要的条件:

    if (window.location.href.toLowerCase() == 'http://test.testdomain.com/news') {
        $('#main').show();
    }
    

    【讨论】:

    • 这是正确的解决方案 - 您导航到哪个 URL?当您执行console.log(window.location.href); 时,您在浏览器的开发控制台中看到了什么?
    • 我收到 test.testdomain.com/news(包括 http://,但显示为链接)
    • koby...其他人也会通过 indexOf 条件
    • @charlietfl 我认为这就是 OP 的意思......让我看看他的下一条评论。
    • 另一种更短的方式是location.pathname === '/news'
    猜你喜欢
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多