简体   繁体   English

如何使用javaScript访问这些id

[英]How do I access these id's with javaScript

I have website with a javaScript function that should scroll to section on page when user clicks a navigation item. 我有一个带有javaScript函数的网站,当用户单击导航项时,该函数应滚动到页面上的部分。 This script worked before I made changes to my nav menu. 在我更改导航菜单之前,此脚本有效。 I can not figure out how to reference the ID's in the javaScript correctly. 我无法弄清楚如何正确引用javaScript中的ID。

Here is HTML nav menu: 这是HTML导航菜单:

<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle Navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Data Detective</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a id="home" href="#homeSect">HOME</a></li>
                <li><a id="about" href="#aboutSect">ABOUT</a></li>
                <li><a id="portfolio" href="#portfolioSect">PORTFOLIO</a></li>
                <li><a id="contact" href="#contactSect">CONTACT</a></li>
            </ul>
        </div>
    </div>
</div>

Here is the javaScript: 这是javaScript:

$(document).ready(function() {
setBindings();
});


//Allow screen to Scroll to selected section
function setBindings() {
    $("nav ul li a").click(function (tip) {
        tip.preventDefault();
        var sectionID = "#" + tip.currentTarget.id + "Sect";
        alert('button id ' + sectionID);

        $("html body").animate({
            scrollTop: $(sectionID).offset().top
        }, 1000);
    });
}

You should use .navbar class. 你应该使用.navbar类。 Please change: 请更换:

 $("nav ul li a").click(function (tip) {

TO

$(".navbar ul li a").click(function (tip) {

Further more, I recommend you to use var sectionID = $(this).attr('href'); 此外,我建议你使用var sectionID = $(this).attr('href'); instead var sectionID = "#" + tip.currentTarget.id + "Sect"; 而是var sectionID = "#" + tip.currentTarget.id + "Sect"; because it's more simply. 因为它更简单。

Your jQuery selector: 你的jQuery选择器:

$("nav ul li a")

will search for the element <nav> (which doesn't exist). 将搜索元素<nav> (不存在)。

Instead, you can use the selector: 相反,您可以使用选择器:

$(".nav a")

I would do a more useful global function I guess. 我想我会做一个更有用的全局函数。 I would check for any href with an anchor reference, check if there is a div with that id, and then scroll to that one. 我会检查任何带有锚引用的href,检查是否有一个带有该id的div,然后滚动到那个。

That way you're not limited to the menu but can use the same code everywhere. 这样你不仅限于菜单,而且可以在任何地方使用相同的代码。 Also I made the event caputurer the top level document, so you can insert and remove elements at hearts content and it will always work without having to be rebound. 此外,我将事件caputurer作为顶级文档,因此您可以在心脏内容中插入和删除元素,它将始终工作而不必反弹。

 $(document).on('click','a[href^="#"]',function(e) { var target = $(e.currentTarget).attr('href'); var $target = $(target); if($target.length > 0) { $("html body").animate({ scrollTop: $target.offset().top }, 1000); } }); 
 div.sect { margin:50px; height:400px; width:100%; background-color:gray; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li><a id="blah" href="www.google.nl">Excluded</a></li> <li><a id="home" href="#homeSect">HOME</a></li> <li><a id="about" href="#aboutSect">ABOUT</a></li> <li><a id="portfolio" href="#portfolioSect">PORTFOLIO</a></li> <li><a id="contact" href="#contactSect">CONTACT</a></li> <li><a id="blah" href="www.google.nl">Excluded</a></li> <div class="sect" id="portfolioSect"> portfolioSect </div> <div class="sect" id="homeSect"> homeSect </div> <div class="sect" id="aboutSect"> aboutSect </div> <div class="sect" id="contactSect"> contactSect </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何定义javascript变量作为div的ID? - How do I define a javascript variable as a div's id? 如何使用 javascript 枚举文档中的所有 html id? - How do I enumerate all of the html id's in a document with javascript? 如何从用户脚本访问iframe的javascript? - How do I access an iframe's javascript from a userscript? Javascript - 如何使用每个元素的父级 unique_id 定位所有内部 class - Javascript - How do I target all the inner class with it's parent's unique_id for each element Javascript:如何从JQuery Dialog构造函数中的类描述符访问事件的ID - Javascript: How can I access an event's id from a class descriptor from within a JQuery Dialog constructor 如果ASP.NET破坏其ID,我如何从javascript访问DIV? - How do I access a DIV from javascript, if ASP.NET mangles its ID? 如何使用id从javascript访问HTML元素,该html元素属于另一个html文档 - how do I access a HTML element from javascript using id,the html element belongs to another html document 如何使用 JavaScript 访问 asp.net 母版页中的元素 ID? - How do I access the element ID in an asp.net masterpage with JavaScript? 如何在 JavaScript 中将 ID 更改为类 - How do I change an ID to a Class in JavaScript 如何将 id 值增加 1 (Javascript) - How do I increase a id value by 1 (Javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM