编写以下内容的更好或正确的方法是什么:
<a href="javascript:void(0)" onclick ="$('p').show()">click here</a>
If you're using jQuery, the proper way would be:
html
<a href="#">Link</a>
jQ
$('a').click(function(e){ e.preventDefault(); $('p').show(); });
完全忽略href
:
<a onclick ="$('p').show()>click here</a>
Since you're using jQuery, use it at its full potential:
<a id="your-id">click here</a>
<script>
$('#your-id').click(function() {
$('p').show();
});
</script>
Use #
and return false
in the onclick handler.
return false
prevents the URL from being followed. An anchor to #
points to the current page, so that it makes sense to open/bookmark the link.
<a href="#" onclick="$('p').show();return false;">click here</a>
The semantically correct thing to do here is to use a button tag instead of an a tag. It is bad practice to use javascript:void(0) in a link. Shoot, it's bad practice to include any inline JavaScript.
Let it point to an URL which will make the desired element to show up by a server side view technology such as PHP/JSP/ASP so that the link still works for clients who have JS disabled.
Eg in JSP:
<a href="currentPage.jsp?foo=1" class="foo">link</a>
<p class="bar ${param.foo != 1 ? 'hide' : ''}">paragraph</p>
with
$(".foo").click(function() {
$(this).next(".bar").show();
return false;
});
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.