最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - how to redirect to a division(<div>) using javascript function in my html page - Stack Overflow

programmeradmin3浏览0评论

My problem is that I want to redirect within my same html page which contain a div named add-form.

When I click on the button my redirection should be on one div which contains some code. Here is some of my code that redirects to another page, but I have no idea how to replace the page with a div instead.

 success: function(data){

          if(data['success']){
        alert("DOne");  

        window.location = document.getElementById('add-form');
         // $("#add-form").load(function(e) {

         // });


          }
          else{
              alert("Oops, There is a Network Error.....");
          }
      }

In this code #add-form is DIV id.

I don't want to redirect to another page but to slide in the page that contain div tag.

UPDATE:

here are the two images.

In the first image click of + button which held in right side up. code is :

<a href="#add-form" data-icon="plus" data-iconpos="notext">Add</a>

result is this image.

I want to do the same for a button click, but in javascript.

My problem is that I want to redirect within my same html page which contain a div named add-form.

When I click on the button my redirection should be on one div which contains some code. Here is some of my code that redirects to another page, but I have no idea how to replace the page with a div instead.

 success: function(data){

          if(data['success']){
        alert("DOne");  

        window.location = document.getElementById('add-form');
         // $("#add-form").load(function(e) {

         // });


          }
          else{
              alert("Oops, There is a Network Error.....");
          }
      }

In this code #add-form is DIV id.

I don't want to redirect to another page but to slide in the page that contain div tag.

UPDATE:

here are the two images.

In the first image click of + button which held in right side up. code is :

<a href="#add-form" data-icon="plus" data-iconpos="notext">Add</a>

result is this image.

I want to do the same for a button click, but in javascript.

Share Improve this question edited Dec 19, 2013 at 22:35 Greg Giacovelli 10.2k2 gold badges49 silver badges64 bronze badges asked Dec 17, 2013 at 10:31 AndyBoyAndyBoy 5946 silver badges29 bronze badges 6
  • Do you just want the browser to scroll to the Div when the user clicks a link? – Scott Helme Commented Dec 17, 2013 at 10:35
  • $("#add-form").get(0).scrollIntoView(); => this should work for you – Talha Masood Commented Dec 17, 2013 at 10:35
  • 1 Why do you need to do it in JS? Why not just <a href="#add-form">Click</a> – Scott Helme Commented Dec 17, 2013 at 10:36
  • @TalhaMasood @ScottHelme I need to open slide panel when condition is satisfied in a onclick event and the slide panel is a <DIV>. – AndyBoy Commented Dec 17, 2013 at 10:38
  • Then that means you need to animate a div visible if the condition is satisfied. Keep the div hidden by default, and make it visible by animation if the condition is satisfied. – Talha Masood Commented Dec 17, 2013 at 10:42
 |  Show 1 more ment

2 Answers 2

Reset to default 5

I think what you might be looking for is this:

<a href="#add-form">Click</a>

When a user clicks that link, they will be scrolled to the div with id="add-form".

Edit

I can't test right now, but try something like this to do it in JS:

$("#buttonToClick").click(function() {
    $('html, body').animate({
       scrollTop: $("#add-form").offset().top
    }, 2000);
});

If you really must do it in JS (although, I cannot see why you wouldn't use Scott Helme's solution), use this line to anchor your browser to the position of the div with the id "add-form":

window.location.href = '#add-form';

Make sure to prepend the hash (#) to the element's ID.

发布评论

评论列表(0)

  1. 暂无评论