admin管理员组文章数量:1317723
I am creating a list of anchor tags from a MySQL/PHP query; the anchor tags call a JavaScript function.
The 'catch 22' that I have is:
href="#"
makes the page jump to the top every time one of the anchor tags is clicked (very annoying)- removing
href="#"
means that the cursor does not change as an anchor tag is hovered over nor does that anchor tag have the appearance of an anchor tag.
I know there is a way to handle this with JavaScript (possibly jQuery) but I don't recall how at the moment. However, I really prefer a simpler HTML fix (if one exists) that does not require me to get into JavaScript.
Edit:
"does not require me to get into JavaScript" == "does not require extensive changes in JavaScript."
I am creating a list of anchor tags from a MySQL/PHP query; the anchor tags call a JavaScript function.
The 'catch 22' that I have is:
href="#"
makes the page jump to the top every time one of the anchor tags is clicked (very annoying)- removing
href="#"
means that the cursor does not change as an anchor tag is hovered over nor does that anchor tag have the appearance of an anchor tag.
I know there is a way to handle this with JavaScript (possibly jQuery) but I don't recall how at the moment. However, I really prefer a simpler HTML fix (if one exists) that does not require me to get into JavaScript.
Edit:
"does not require me to get into JavaScript" == "does not require extensive changes in JavaScript."
-
You can just set
cursor: pointer;
on <a> tags if you need to (and other hover styles). This is CSS though, not HTML. – TylerH Commented Dec 10, 2020 at 16:47
5 Answers
Reset to default 4In your javascript function you should return false
, or with jquery you can use preventDefault()
Example:
$('a').click(function(event) {
event.preventDefault();
// do something
});
Or in your case:
<a href=“#” onclick="foo();return false;">
Or change the href to javascript:void(0)
:
<a href="javascript:void(0)" onclick="foo();">
Ideally, your link degrades without javascript, so the third option will usually be avoided.
A simpler HTML fix for you: Personally speaking, for plain test links I just use href=""
For links that point to a javascript function I tend to use href="javascript:;"
. Either way you'll stop the page jumping.
do return false after onclick or from within the foo() function.
You need to call event.preventDefault();
somewhere in your click event handler. In vanilla javascript, this can be done like so:
<script type="text/javascript">
function foo(e) {
e.preventDefault();
// other code
}
</script>
<a href="#" onclick="foo(e);"></a>
Additionally, you can also return false:
<a href="#" onclick="foo();return false;/>
This can be also done in jQuery pretty simply:
$('.mylink').click(function(e) {
e.preventDefault();
// other code
});
The downside of using return false;
is that is also prevents the event from bubbling up the DOM. If you don't care about event bubbling it's okay to use false, but personally I feel that it's better to use event.preventDefault();
unless you specifically want to stop event bubbling as well.
<html>
<head>
</head>
<body>
<style>
a {
text-decoration: underline;
}
a:hover {
text-decoration: underline;
}
</style>
<script type="text/javascript">
function foo() {
alert('Hello World');
}
</script>
<a onclick="foo();">Click Here</a>
</body>
</html>
Just adding the CSS and removing the href
seems fine.
本文标签: javascriptAnchor tags list creationStack Overflow
版权声明:本文标题:javascript - Anchor tags list creation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742022270a2414894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论