2016-03-15 94 views
31

如何將一個鏈接放在一個帶有引導的按鈕上?如何通過引導將按鈕鏈接到按鈕上?

有4種方法引導文件中:

<a href="#" class="btn btn-info" role="button">Link Button</a> 
<button type="button" class="btn btn-info">Button</button> 
<input type="button" class="btn btn-info" value="Input Button"> 
<input type="submit" class="btn btn-info" value="Submit Button"> 

第一個不爲我工作,沒有按鈕節目,只要有鏈接的文本,都使用感受它的主題IM。

第二個顯示的是我想要的按鈕,但是什麼代碼使按鈕鏈接到另一個頁面時單擊?

乾杯

+1

https://jsfiddle.net/Lstcymqo/問題可能與您正在使用的主題 – linktoahref

+0

[按鈕和鏈接之間有什麼區別?](https://ux.stackexchange.com/q/5493) –

回答

18

您可以在按鈕的點擊事件中調用一個函數。

<input type="button" class="btn btn-info" value="Input Button" onclick=" relocate_home()"> 

<script> 
function relocate_home() 
{ 
    location.href = "www.yoursite.com"; 
} 
</script> 

OR 使用此代碼

<input type="button" class="btn btn-info" value="Input Button" onclick="location.href = 'http:\\www.google.com';"> 
65

如果你不真正需要的按鈕元素,只需移動類常規鏈接:

<div class="btn-group"> 
    <a href="/save/1" class="btn btn-primary active"> 
     <i class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></i> Save 
    </a> 
    <a href="/cancel/1" class="btn btn-default">Cancel</a> 
</div> 

相反,您可以還改變按鈕以顯示鏈接:

<button type="button" class="btn btn-link">Link</button> 
+7

比接受的答案好得多 - 不需要JavaScript ..謝謝 –

+1

在使用按鈕時以及使用鏈接時,請注意語義和可訪問性。一般來說,按鈕可用於頁面上的操作,還可以鏈接頁面中的內容或導航離開頁面。 –

35

最簡單的解決方案是你的例子第一個:

<a href="#link" class="btn btn-info" role="button">Link Button</a> 

它不爲你工作的原因是最有可能的,因爲你說了,你正在使用的主題有問題。沒有理由爲此使用臃腫的額外標記或內聯Javascript。

+1

這是最容易的。 –

1

另一個技巧,以獲得鏈接的顏色<button>標記內正常工作

<button type="button" class="btn btn-outline-success and-all-other-classes"> 
    <a href="#" style="color:inherit"> Button text with correct colors </a> 
</button> 

請記住,在BS4例如測試btn-outline-primary改爲btn-outline-primary

0

結合上述答案我找到一個簡單的解決方案,可能會幫助你太:

<button type="submit" onclick="location.href = 'your_link';">Login</button> 

只需添加內聯JS代碼,你可以在一個鏈接轉換按鈕並保持他的設計。