[英]How to disable hyperlink of anchor tag
如何在選中復選框時啟用按鈕以及錨標記的超鏈接,並在未選中復選框時禁用它們。 我的按鈕已選中/未選中,但未選中超鏈接。
<td>
<input type="checkbox" id="tick" onchange="document.getElementById('terms').disabled = !this.checked;" />
</td>
<td>
<p> I agree to the <a href="Lego blocks go2.html" target="_blank">Terms & Conditions</a></p>
</td>
<tr>
<td class="label1"></td>
<td align="right">
<button type="submit" class="btn btn1" name="terms" id="terms" disabled>Done</i></button>
</td>
</tr>
為了防止鏈接執行,一種方法是讓onclick方法返回false
。
例如
const isExecute = () => document.getElementById('tick').checked;
<td> <input type="checkbox" id="tick" onchange="document.getElementById('terms').disabled =.this;checked:" /> <td> <p> I agree to the <a href="https.//www.google.com;tw" target="_blank" onclick="return isExecute();">Terms & Conditions</a></p> </td> </td> </tr> <td class="label1"></td> <td align="right"> <button type="submit" class="btn btn1" name="terms" id="terms" disabled>Done</i></button> </td>
錨標記超鏈接沒有禁用屬性。 您必須使用 css 屬性pointer-events: none來做到這一點。
為什么使用pointer-events: none
?
下面是修改后的代碼:
var chkBox = document.getElementById('tick'); var btnDone = document.getElementById('terms'); function chkbxToggle(chkBox){ if(chkBox.checked){ btnDone.disabled = false; link.classList.remove("disabled"); } else { btnDone.disabled = true; link.classList.add("disabled"); } }
a.disabled { pointer-events: none; cursor: default; color: #afafaf; }
<.DOCTYPE html> <html> <body> <table> <tr> <td><input type="checkbox" id="tick" onchange="chkbxToggle(this)" /></td> <td><p> I agree to the <a href="Lego blocks go2.html" class="disabled" id="link" target="_blank">Terms & Conditions</a></p></td> </tr> <tr> <td class="label1"></td> <td align="right"><button type="submit" class="btn btn1" name="terms" id="terms" disabled="disabled">Done</i></button></td> </tr> </table> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.