window.location.href附加而不是替换

时间:2016-06-22 22:29:18

标签: javascript jquery html css web

我有一个小问题,我无法弄清楚。我有一个滑块,当我点击prevnext按钮时,我想更改网址。以下是我的代码,但它无法正常工作。每次我点击按钮,网址都会附加而不是替换。

var url = window.location.href;
url = url + "page/" + 1;  // this number is dynamic actually
window.location.href = url;

例如它显示; stackoverflow.com/page我点击stackoverflow.com/page/1并再次点击stackoverflow.com/page/1/page/2

但我只想将其stackoverflow.com/page/1替换为stackoverflow.com/page/2

我该如何解决?谢谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您想要的只是/前面page的前导var url = "/page/" + 1; 来建立域相对路径

{{1}}

答案 1 :(得分:2)

window.location.href返回整个网址...

如果您只想添加" / page / 1"

使用

var url = window.location.origin;
url = url + "/page/" + 1;  // this number is dynamic actually
window.location.href = url;

虽然在Windows 10中未定义window.location.origin

https://developer.mozilla.org/en-US/docs/Web/API/Window/location

你可以直接替换/ page /#number#

var url = window.location.href;
url = url .replace(new RegExp("/page/[0-9]"), "/page/2")
window.location.href = url;

答案 2 :(得分:0)

您可以使用正则表达式获取页面部分并替换。

类似的东西:

#include "stdafx.h"
#include <cstdlib>
#include<iostream>
#include <queue>
using namespace std;

const int max_size = 10;


class Stack{

public:

    Stack(int max_size);

    int max_size;

    void Add(int data);
    int Extract();

    void Show();

private:

    struct node {
        int data;
        node* next;
    };

    node*head;
    node*tail;

    void push(node *& head, node *&tail, int data) {
        if (head == NULL) {
            node* n = new node;
            n->data = data;
            n->next = NULL;
            head = n;
            tail = n;

        }
        else if (head != NULL) {
            node* n = new node;
            n->data = data;
            n->next = head;
            head = n;
        }
    }

    void showdata(node *& head) {
        node* temp = new node;
        temp = head;
        if (temp == NULL) {
            cout << "Empty" << endl;
        }
        else {
            cout << "element: " << endl;
            while (temp != NULL) {
                cout << temp->data << endl;
                temp = temp->next;
            }
        }
    }

    void pop(node *&head, node *& tail) {
        if (head == NULL) {
            cout << "Empty" << endl;
        }
        else if (head == tail) {
            cout << "value: " << head->data << " was popped" << endl;
            delete head;
            head = NULL;
            tail = NULL;
        }
        else {
            node* delptr = new node;
            delptr = head;
            head = head->next;
            cout << "value: " << delptr->data << " was popped" << endl;
            delete delptr;
        }

    }
};

Stack::Stack(int _max_size)
{
    max_size = _max_size;
    head = NULL;
    tail = NULL;
}



void Stack::Add(int data)
{
    push(head, tail, data);
}

int Stack::Extract()
{

    int poppedvalue = head->data;
    pop(head, tail);

    return poppedvalue;
}

void Stack::Show()
{
    showdata(head);
}



int main()
{
    //intialize
    Stack stack(max_size);

    cout << "Testing Stack" << endl;    

    stack.Show();

    for (int i = 0; i < stack.max_size; i++)
        stack.Add(i);

    stack.Show();

    for (int i = 0; i < stack.max_size; i++)
        cout << stack.Extract() << endl;

    return 0;
}