Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

javascript - 如何将按钮重定向到另一个页面? [重复](How can I make a button redirect my page to another page? [duplicate])

This question already has an answer here:

(这个问题在这里已有答案:)

I have been trying the following:

(我一直在尝试以下方面:)

<form action="/home" class="inline">
    <button class="float-left submit-button" >Home</button>
</form>

It seems to work but it goes to the page "/home?"

(它似乎工作,但它进入页面“/ home?”)

Is there a better way for me to make a button inside a form make the page go to a new location?

(有没有更好的方法让我在表单中创建一个按钮使页面转到新位置?)

  ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just add an onclick event to the button :

(只需在button添加一个onclick事件:)

<button onclick="location.href = 'www.yoursite.com';" id="myButton" class="float-left submit-button" >Home</button>

But you shouldn't really have it inline like that, instead, put it in a JS block and give the button an ID:

(但是你不应该像这样内联它,而是将它放在一个JS块中并给button一个ID:)

<button id="myButton" class="float-left submit-button" >Home</button>

<script type="text/javascript">
    document.getElementById("myButton").onclick = function () {
        location.href = "www.yoursite.com";
    };
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...