How to create an HTML button that acts like a link to an item on the same page?

I would like to create an HTML button that acts like a link to an item on the same page. So, when you click the button, it redirects to item on the same page.

How can I do this? (I would limit the solution to HTML, CSS, and JavaScript, because currently I am not using any other language)

Current Button (Bootstrap):

<a class="btn btn-large btn-primary" href="">Democracy</a>

This is assuming that you are not talking about scrolling down to a regular anchor, and instead you want to scroll to actual HTML elements on the page.

I'm not sure if jQuery counts for you, but if you're using Bootstrap, I imagine it does. If so, you can bind to the "click" event for your button and put some javascript code there to handle the scrolling. Typically you might associate the link/button with the element you want to scroll to using a "data" attribute (eg data-scroll="my-element-id").

Without jQuery, you'll have to make a function that contains the code as described, and put in an onclick attribute that references your function, and passes "this" as a parameter to your function, so you can get the reference to the link/button element that called it.

For the code to use to actually scroll to the corresponding element, check out this article:

How to go to a specific element on page?

Quick example without jQuery:

<a class="scrollbutton" data-scroll="#somethingonpage" 
 onchange="scrollto(this);">something on page</a>

<div id="somethingonpage">scrolls to here when you click on the link above</a>

<script type="text/javascript">
    function scrollto(element) {
        // get the element on the page related to the button
        var scrollToId = element.getAttribute("data-scroll");
        var scrollToElement = document.getElementById(scrollToId);
        // make the page scroll down to where you want
        // ...
    }
</script>

With jQuery:

<a class="scrollbutton" data-scroll="#somethingonpage">something on page</a>

<div id="somethingonpage">scrolls to here when you click on the link above</a>

<script type="text/javascript">
    $(".scrollbutton").click(function () {
        // get the element on the page related to the button
        var scrollToId = $(this).data("scroll");
        var scrollToElement = document.getElementById(scrollToId);
        // make the page scroll down to where you want
        // ...
    });
</script>

Note: If you literally want a "button" rather than a "link", you can really use any element and make that clickable, eg:

<button class="scrollbutton" data-scroll="#somethingonpage">something on page</button>

尝试:

<button onclick="window.location.href='location'">Button Name</button

hey try this : -

  <button><a href="#A">Click Me</a></button>

then to which ever place you want to go in your site : -
u may just place the line below wherever you want,

  <a name="A"></a>

hope it works for you

链接地址: http://www.djcxy.com/p/6232.html

上一篇: 如何在html上的新标签中打开链接?

下一篇: 如何创建一个HTML按钮,就像链接到同一页上的项目一样?