简体   繁体   English

如何通过jquery实现“跳转”?

[英]How can I achieve “jumps” with jquery?

I am developing a website that loads different pages depending on what menu icon a user clicks. 我正在开发一个网站,该网站根据用户单击的菜单图标加载不同的页面。 A friend and I wrote this javascript function: 我和一个朋友写了这个javascript函数:

function loadPage(targetURL){
    $.get( targetURL, function( data ) {
    document.getElementById("placeholder").innerHTML=data;
    window.scrollTo(0,0);
    });
}

And the menu icons are coded like this: 菜单图标的编码如下:

<img src="images/some_icon.png" title="Some Page" onclick="javascript:loadPage('pages/somePage.php')"/>

It works great except if trying to jump to a particular div id. 它非常有用,除非尝试跳转到特定的div ID。 For example, 例如,

<img src="images/some_icon.png" title="Some Page" onclick="javascript:loadPage('pages/somePage#someLocationOnPage.php')"/>

Taking out the .scrollTo(0,0) doesn't solve the issue. 取出.scrollTo(0,0)不能解决问题。 I put that line in to ensure that the page is scrolled all the way up when changing pages (Otherwise, if you were in the middle of the page and clicked a menu icon, the new page would display in the middle of the page). 我放入该行以确保在更改页面时页面一直向上滚动(否则,如果您位于页面中间,并单击菜单图标,则新页面将显示在页面中间)。

What have I missed? 我错过了什么? How can I get the link to jump to a div id? 如何获取链接以跳转到div ID?

The two URL's are not different. 这两个网址没有不同。 The one with the bookmark just gets the same HTML code. 带有书签的代码只会获得相同的HTML代码。 Since you are not telling the browser to go to a specific bookmark, it should display the same information. 由于您没有告诉浏览器转到特定的书签,因此它应该显示相同的信息。 (Unless your server sends different info for that link, of course) (当然,除非您的服务器为该链接发送了不同的信息)

I guess you'd have to manually move the view to the bookmark you want. 我想您必须手动将视图移动到所需的书签。

function loadPage(targetURL, targetID){
    $.get( targetURL, function( data ) {
        document.getElementById("placeholder").innerHTML=data;
        if(targetID && $("#"+targetID).length != 0){
            $(document).scrollTop($("#"+targetID).offset().top);
        } 
        else {
            window.scrollTo(0,0);
        }
    });
}

And then give the ID for the target as another parameter. 然后将目标的ID作为另一个参数。 If that's not possible, you can also search for # in the url and find the ID/ or anchor. 如果不可能,您也可以在网址中搜索#并找到ID /或锚点。

First of all you should keep the window.scrollTo(0,0) in your code to keep the page go to top on every click of menu item. 首先,您应该在代码中保留window.scrollTo(0,0) ,以使页面在每次单击菜单项时都位于顶部。

Secondly if on click of menu icon which is calling 其次,如果点击正在调用的菜单图标

 loadPage('pages/somePage#someLocationOnPage.php')

your intentions are to open 'somePage' and scroll to the position where element id is 'someLocationOnPage.php' then you should update your loadPage method to check for the part after '#'. 您的意图是打开'somePage'并滚动到元素ID为'someLocationOnPage.php'的位置,然后应更新loadPage方法以检查'#'之后的部分。

function loadPage(targetURL){
    $.get( targetURL, function( data ) {
      document.getElementById("placeholder").innerHTML=data;
      window.scrollTo(0,0);

      // Grab url part after '#'
      var name = targetURL.substr( href.indexOf('#') + 1 ), target = $('a[name='+ name +']'), target2 = $('#' + name);

      // Determine of bookmark is available or not
      target = (target.length)? target : target2;

      // Scroll to the bookmark
       $('html,body').animate({
          scrollTop: target.offset().top
       },400);
    });
}

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

相关问题 如何使用Javascript / Jquery实现这些? - How can i achieve the these with Javascript / Jquery? 如何在Jquery中实现真正的随机性? - How can I achieve true randomness in Jquery? 在这种情况下,如何在jquery中实现限制字符? - How can I achieve a limit character in jquery in this situation? 我如何用Lodash做到这一点? - How can I achieve this with Lodash? 我想用很少的控件作为参数在jquery中创建一个自定义函数。 我该如何实现? - I want to create a custom function in jquery with few controls as parameters. How can I achieve this? 如果没有 html 中的 onclick = "showHide (this),但在 jquery 中,如何实现相同的效果? - How can I achieve the same without having the onclick = "showHide (this) in the html, but in jquery? setTimeout()跳到数组的最后一个元素如何防止这种情况? - setTimeout() jumps to last element of array how can i prevent this? 重定向到首页网址后,如何使用Javascript / Jquery实现自动点击? - How can I achieve an autoclick with Javascript / Jquery after being redirected to my homepage url? 如何实现像jQuery的slideDown这样的下滑效果,但只使用CSS? - How can I achieve a slide down effect like jQuery's slideDown, but only using CSS? 如何以最小的复杂度获得结果? - How can I achieve the result with the least complexity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM