[英]How do I get the Id of a hyperlink?
How do I get the id of a hyerlink on a page? 如何获取页面上的超级链接的ID?
Example 例
<a id="myID123" href="myLinkPageName.aspx">myLink</a>
Note: The page name and the link name is static! 注意:页面名称和链接名称是静态的! I should get the id "myID123".
我应该获得ID“ myID123”。
use jquery is very easy 使用jQuery非常简单
$('a').attr('id')
$("a[href='myLinkPageName.aspx']").attr('id')
You can give a class at the hyperlink you might want like 您可以在可能想要的超链接上提供课程
<a id="myID123" href="myLinkPageName.aspx" class="my-links">myLink</a>
and then search for it with jQuery doing the following: 然后使用jQuery进行以下搜索:
$('.my-links').attr('id');
In case you want to get the ids for all your hypelinks in your page you can do the following: 如果您想获取页面中所有超链接的ID,可以执行以下操作:
$('a').attr('id');
You can also do more complex search using the following attributes: 您还可以使用以下属性进行更复杂的搜索:
= is exactly equal
!= is not equal
^= is starts with
$= is ends with
*= is contains
An example might be: 一个示例可能是:
$('a[href*="myLinkPageName"]')
walk through your A-Tags and look for matching href, then return the id 浏览您的A标签并查找匹配的href,然后返回ID
i assume u are using jquery, as we all do :-) 我假设您正在使用jquery,就像我们所有人一样:-)
var foundid = "id not found";
var desired_href = "myLinkPageName.aspx";
$('a').each(function(){
if($(this).attr('href') == desired_href) foundid = $(this).attr('id');
});
alert(foundid);
this solution isn't pretty, but quick 这个解决方案不是很好,但是很快
Non jQuery solution, just for fun 非jQuery解决方案,只是为了好玩
var href_search = "myLinkPageName.aspxmyLinkPageName.aspx";
for (var i; i<document.links.length; i++) {
if (document.links[i].href == href_search) break;
}
var id = document.links[i].id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.