简体   繁体   English

如何重定向到部门( <div> )在我的html页面中使用javascript函数

[英]how to redirect to a division(<div>) using javascript function in my html page

My problem is that I want to redirect within my same html page which contain a div named add-form . 我的问题是我想在我的html页面中重定向,该页面包含一个名为add-form的div。

When I click on the button my redirection should be on one div which contains some code. 当我单击button我的重定向应该在一个包含一些代码的div上。 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. 这是我的一些重定向到另一个页面的代码,但是我不知道如何用div代替页面。

 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 . 在此代码中, #add-formDIV id

I don't want to redirect to another page but to slide in the page that contain div tag . 我不想重定向到另一个页面,而是要在包含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. 我想对按钮单击执行相同操作,但是使用javascript。

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" . 当用户单击该链接时,他们将使用id="add-form"滚动到div。

Edit 编辑

I can't test right now, but try something like this to do it in JS: 我目前无法测试,但可以尝试使用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": 如果您确实必须在JS中执行此操作(尽管,我看不到为什么不使用Scott Helme的解决方案),请使用此行将您的浏览器定位到ID为“ add-form”的div的位置:

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

Make sure to prepend the hash ( # ) to the element's ID. 确保将哈希( )放在元素ID的前面。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM