<a> not working properly

时间:2015-07-23 15:23:26

标签: javascript css asp.net href

I am having some issues with the href's on my web page. When the page loads, I check the screen width, and if it is small enough for mobile I am changing the href of the tags in the header. Here is what the javascript looks like.

$(function () {
    var hrefs = [$('#BTN_Services').attr('href'), 'http://jdcservices.ca' + '#mobileServices'];

    $(window).on('resize', function () {
    $('#BTN_Services').attr('href', function () {
        return hrefs[$(window).width() > 1240 ? 0 : 1];
    });
    }).trigger('resize');
}

This is the code that changes the href from the /Services.aspx page to the #mobileServices div.

Below is my header that I am changing.

<a href="#" id="logo"></a>

<nav>

    <a href="#" id="menu-icon"></a>

    <ul>

        <li><a id="BTN_Home" href="/Index.aspx" class="current">HOME</a></li>
        <li><a href="/Safety.aspx">SAFETY</a></li>
        <li><a id="BTN_Services" href="/Services.aspx">SERVICES</a></li>
        <li><a href="/Careers.aspx">CAREERS</a></li>
        <li><a href="/Contact.aspx">CONTACT</a></li>

    </ul>

</nav>

</header>

<div id="mobileServices"  style="width: 100%; padding-bottom: 5%;">
        <div class="row" style="padding-bottom: 5%;">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <h1 style="text-align: center; font-weight: bold;">Services</h1>
                <hr width="75%" />
                <p>
                    Based in Hinton, AB and servicing Hinton, Edson, Grande Cache, and Fox Creek area, JD&C Services has grown and adapted to meet the needs of its clients with a team of highly skilled individuals.  We strive to perform our jobs in an efficient, safe, and environmentally responsible manner exceeding the expectations of our customers.
                </p>
            </div>
        </div>
        <div class="row" style="text-align: center; height:100%; overflow: hidden;">
            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 sameHeight" style="background-color: #421C4C; color:white;  vertical-align: middle; padding-top: 4%; padding-bottom: 2%;">
                <h2><i class="fa fa-truck"></i>&nbsp;&nbsp; Tank Truck Services</h2>
                <hr width="75%"/>
                <p>
                    - Sour sealed tank truck services
                    <br />
                    - Body jobs
                    <br />
                    - Tri-Tri's
                    <br />
                    - Super B's
                    <br />
                    * All trucks are equipped with GPS tracking hardware.
                </p>
            </div>
            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 sameHeight" style="background-color: white; color:black;  vertical-align: middle; padding-top: 2%; padding-bottom: 2%;">
                <h2><i class="fa fa-database"></i>&nbsp;&nbsp;Product Sales</h2>
                <hr width="75%"/>
                <p>
                    - KCL sales
                    <br />
                    - Clay stabalizer
                    <br />
                    - Hot and cold water sales
                    <br />
                    - Kill fluid
                </p>
            </div>
            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 sameHeight" style="background-color: #421C4C; color:white; vertical-align: middle; padding-top: 2%; padding-bottom: 4%;">
                <h2><i class="fa fa-road"></i>&nbsp;&nbsp;Portable Filter Units</h2>
                <hr width="75%"/>
                <p>
                    Portable filter units are available for delivery and rental. Various micron filters are available.
                </p>
            </div>
        </div>
        <br />
    </div>

So from what I can inspect inside google chrome, is that the href is changing as expected, but the href will still not take me to the correct div. I am not sure what I am doing, maybe it is something preventing it in my CSS?

Here中的Href是我正在处理的网站

2 个答案:

答案 0 :(得分:2)

我从您的网站上可以看出,您正在使用JavaScript来使用Ajax导航页面(使用华丽的动画)。我猜测代码在事件上调用preventDefault来阻止默认操作,在这种情况下这不是你想要的。

答案 1 :(得分:1)

我不知道为什么链接不会跳转到锚位置,我尝试删除所有点击处理程序,但它没有工作,但你可以这样修复:

当您的网页位于移动版本时添加此内容:

$('#BTN_Services').click(function({window.location.href="#mobileServices";});

以这种方式删除:

$('#BTN_Services').off('click');