简体   繁体   English

在jQuery / Javascript中上一个“文件夹”或段

[英]Go up one “folder” or segment in jQuery / Javascript

If I'm, for example, on this page 例如,如果我在这个页面上

www.example.com/admin/bridge/boilerplate

What is the best way (Using plain javascript, or jQuery (Without loading another plugin) to go up one level, eg 什么是最好的方法(使用普通的javascript或jQuery(不加载另一个插件)上升到一个级别,例如

www.example.com/admin/bridge

At the moment we are using 目前我们正在使用

window.history.go(-1);

which interferes with submitted forms, etc. This is used normally on a function like this: 这会干扰提交的表格等。这通常用于这样的功能:

$("button.cancel").bind("click", function( e ){
    window.history.go(-1);
    e.preventDefault();
});

Simple: 简单:

var url = window.location.href;

if (url.substr(-1) == '/') url = url.substr(0, url.length - 2);

url = url.split('/');
url.pop();

window.location = url.join('/');
var i = window.location.href.lastIndexOf("/");
window.location = window.location.href.substr(0,i)

thanks, but this edited: 谢谢,但这个编辑:

$("#back a").click(function() {
    var url = window.location.href;

    if (url.substr(-1) == '/') url = url.substr(0, url.length - 2);

    url = url.split('/');
    url.pop();

    window.location = url.join('/');
});

window.history is actually manipulating what is in your browser history, it is what the back/forward buttons in the browser use. window.history实际上是在操纵浏览器历史记录中的内容,它是浏览器中后退/前进按钮使用的内容。

So, if the only way to get to the directory you are in is to navigate to it by digging down through the parent directories, this would work. 因此,如果到达您所在目录的唯一方法是通过向下浏览父目录来导航到它,这将起作用。 However, that is never the only way to get to a page. 但是,这绝不是访问页面的唯一方法。 Users are always able to bookmark the page. 用户始终能够为页面添加书签。

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

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