简体   繁体   English

在元素中忽略href属性?

[英]Omitting the href attribute in an a element?

So many times writing html code along with javascript, I create <a> elements. 如此多次与javascript一起编写html代码,我创建了<a>元素。

My question is, can I ignore the href attribute? 我的问题是,我可以忽略href属性吗? as I rather add an ID attribute and handle the click there. 因为我宁愿添加一个ID属性并在那里处理点击。

Most of the time when I do that, its not to go to a different page, but more to perform a ajax call of some sort. 在大多数情况下,我不会去其他页面,而是更多地执行某种ajax调用。

I hate having the # in the url, it just looks ugly. 我讨厌网址中有#,看起来很丑。

Should I rather use a different element, ie a <span> and just modify it with a class to look like link? 我是否应该使用其他元素(即<span>并仅使用类对其进行修改以使其看起来像链接?

Thanx J 坦克斯

If you have a link without a href , it's not a link, it's probably a button , and you should be using one, instead of a link. 如果您有一个没有href的链接,那么它不是一个链接,它可能是一个按钮 ,您应该使用一个按钮而不是链接。

Button VS. 按钮VS。 Link - User Experience.SE 链接-用户体验

if you dont want use any value in href want 如果您不想在href want中使用任何值

<a href="javascript:void(0)"​ >link</a>  

if dont want to add href but want pointer style of cursor 如果不想添加href但想要光标的指针样式

<a style="cursor:pointer">link</a> 

Yes! 是! It's not necessary to use href attribute if the purpose is to achieve only click event and not link to some page or tab. 如果目的是仅实现单击事件而不link到某些页面或选项卡,则不必使用href属性。

You can style the a tag with CSS by some classname or directly a{ cursor: pointer; color: blue;} 您可以将样式a与一些CSS标签classname或直接a{ cursor: pointer; color: blue;} a{ cursor: pointer; color: blue;}

But you can not style a element using a:link if the href attribute is not assigned 但是,如果未分配href属性,则无法使用a:link a元素设置样式

You don't necessarily need an href with an a tag. 你不一定需要一个href有a标签。 It basically distinguishes it from the other and makes it look clickable. 基本上可以将其与其他进行区分,并使它看起来可点击。 You can use a or any other element for that matter with an onclick for your purpose. 您可以为此目的使用a或任何其他元素,并带有onclick。

<a class="lookClickable" onclick="doSomething()">Click Me</a>

(the above could be anything other than a too like span , div etc. (以上可以得到比其他任何a太像spandiv等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM