繁体   English   中英

如何在 mvc 中单击按钮重定向到另一个页面和特定 div

[英]How to redirect to another page and specific div on button click in mvc

我有一个名为 Page1 的页面,其中有一个按钮。

<button onclick="redirecttodivofotherpage(); "></button>

另一个 Page2 有 3 个 Div

<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>

我想在单击 Page1 按钮时重定向到 div3。
如何使用控制器或 jquery 做到这一点。

你可以尝试这样的事情:

<button class="js-btn"></button>

$(function(){
    $(".js-btn").on("click",function(){
        window.location = "..../#div3";
    });
})

字符串"..../#div3"代表页面的相对 url,最后有一个#div3 这样,使用window.location ,您将被重定向到您想要的页面,并使用#div3到您想要的部分。

这可以通过 cookie 来完成。 设置一个带有你想要滚动的 id 的 cookie,然后,当新页面加载时,读取 cookie 并滚动到定义的 id。 我使用了非常流行的插件jquery-cookie

检查此示例解决方案 注意:单击事件导航到其他页面。

**http://plnkr.co/edit/hBJj69nP6kvrEuoCVw3k?p=preview**

试试这个工作演示,它会起作用

 <!DOCTYPE html> <html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> </head> <body> <button class="click">Click Me</button> <div id="mydiv" style="border:2px solid black;width:800px;height:900px; background-color:orange; position:absolute;top:1000px;margin:20px;"> hello anuradh </div> </div> <script type="text/javascript"> $(document).ready(function(){ $(".click").on('click',function(){ window.location = "#mydiv"; }); }); </script> </body> </html>

否则你可以像这样很好地滚动它

 <!DOCTYPE html> <html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> </head> <body> <button class="click">Click Me</button> <div id="mydiv" style="border:2px solid black;width:800px;height:900px; background-color:orange; position:absolute;top:1000px;margin:20px;"> hello anuradh </div> </div> <script type="text/javascript"> $(document).ready(function(){ $(".click").on('click',function(){ //window.location = "#mydiv"; $('html, body').animate({ scrollTop: $("#mydiv").offset().top }, 2000); }); }); </script> </body> </html>

使用 window.location.hash 滚动到带有 id 的元素

<button class="js-btn"></button>

$(function(){
    $(".js-btn").on("click",function(){
        window.location.hash = "#div3";
    });
});

试试这个,它对我有用:

<a class="className">link</a>

$(".className").on("click", function () {
    window.location = "yourPage.html#divId"; 
});

暂无
暂无

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

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