开发者

How do I get the Id of a hyperlink?

How do I get the id of a hyerli开发者_开发问答nk on a page?

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".


use jquery is very easy

$('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:

$('.my-links').attr('id');

In case you want to get the ids for all your hypelinks in your page you can do the following:

$('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

i assume u are using jquery, as we all do :-)

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

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜