仅当启用 JavaScript 时,如何才能在 Anchor 标记中调用 Onclick,以防禁用 Javascript 然后执行 href?
The JavaScript can only run if JavaScript is enabled.
The href will be followed automatically when a link is clicked on (that's the default behaviour) unless JavaScript is used to prevent it.
function onClick(event) { event.preventDefault(); console.log("JavaScript function called"); } const link = document.querySelector("a"); link.addEventListener("click", onClick);
<a href="http://example.com/">Link</a>
kindly elaborate why do you want to check for java-script disable for executing href. For checking java-script disable you can refer below link for your reference.
How to detect if JavaScript is disabled?
Edit: if javascript is disabled then function written in html will fail automatically and href will gets executed. Please check with below code.
<!DOCTYPE html>
<html>
<body>
<script>
document.write("Hello World!")
function test()
{
alert('okoko');
return false;
}
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
<a href="http://google.com" onclick="return test();">test</a>
<p onclick="">A browser without support for JavaScript will show the text inside the noscript element.</p>
</body>
</html>
as per above screenshots it is confirm that href will gets executed automatically when java-script is disabled.
Hope this answer will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.