简体   繁体   English

获取<a>带有哈希值</a>的锚标记( <a>)</a>的href

[英]Get the href for an anchor tag (<a>) with hash

I have this link: 我有这个链接:

<a href="#main" /></a>

I want the href of the link. 我想要链接的href。 How can I get with jQuery the href of the link. 如何使用jQuery获得链接的href。 When I try this: 当我尝试这个:

var container = $(button).attr('href');
console.log(container);

I only get the # and not the main word. 我只得到#而不是主要词。 What i doing wrong? 我做错了什么?

Try this: 尝试这个:

$('a').attr('href');

If you want to chose a more specific add a class to it like this <a href="#main" class="main"/></a> 如果要选择更具体的内容,请向其添加一个类,例如<a href="#main" class="main"/></a>

and then to get then to get the href do this $('a.main').attr('href'); 然后要获取href来执行$('a.main').attr('href');

Do This 做这个

var container = $('a').attr('href');

or 要么

<a href="#main" id="button"> </a>

and

var container = $('a#button').attr('href');

This will solve your problem 这将解决您的问题

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

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