我一直在尋找在互聯網上的解決方案,爲什麼IE7是不是如正常打開鏈接CSS按鈕沒有在IE工作,但確實在FF
<a href="http://www.google.com"><button class="class1">Google</button></a>
IE7是否不喜歡有?
我聽說我應該爲此使用jquery嗎?但沒有人鏈接到任何文章。
我一直在尋找在互聯網上的解決方案,爲什麼IE7是不是如正常打開鏈接CSS按鈕沒有在IE工作,但確實在FF
<a href="http://www.google.com"><button class="class1">Google</button></a>
IE7是否不喜歡有?
我聽說我應該爲此使用jquery嗎?但沒有人鏈接到任何文章。
根據W3C的anchor (<a>
) tags和<button>
tags規格,你應該能夠做到這一點很好,但是according to a quick Google search,您不能和/或不應該這樣做,並且它在Internet Explorer中不起作用。
This article居然建議添加JavaScript,因此該鏈接可以在IE也將拉開:
<a href="http://www.expertsguide.info/"><button type="button" onclick="window.location('http://www.expertsguide.info/')">Click Me to go to Experts Guide</button></a>
你最好不要在超鏈接中使用按鈕。
將超鏈接設置爲按鈕樣式。
嘗試這些
雖然你可以,你不應該有內部的錨(<a>
)按鈕(<button>
)。
添加事件處理程序的按鈕,像這樣:
<input type="button" value="Google" onClick="javascript:location.href = 'http://google.com';" />
注意:你應該認爲不這樣做,對於各種各樣的原因。底線你可以(也應該)將你的<a>
元素看起來像一個按鈕。
根據W3C,可以有形式的元件(其包括''
@Nightfirecat你說得對。它可以完成,但不應該完成。據此編輯。 – Joao
試試這個:
<script>
$(document).ready(function() {
$('.class1').click(function() {
window.location = $(this).parent().attr('href');
return false;
}
});
</script>
或者乾脆刪除按鈕標籤和使用:
<script>
$(document).ready(function() {
$('a').click(function() {
window.location = $(this).attr('href');
return false;
}
});
</script>
要JavaScript的獨立(因此它也適用於瀏覽器與JS禁用,在出乎很多其他的答案在這裏),我建議用普通的方法將它包裝在<form>
中,並改爲<button type="submit">
(或<input type="submit">
)。
<form action="http://www.google.com">
<button type="submit" class="class1">Google</button>
</form>
或
<form action="http://www.google.com">
<input type="submit" value="Google" class="class1" />
</form>
謝謝,這是我使用的解決方案,除了某些原因,它必須是window.location.href ...怪異不是嗎? – Matt