开发者

Get HREF from unspecified anchor tag

I have a list of auto-generated anchor tags from my code-behind which HREFs are a combination of # and the ID from an object extracted from a database.

What I need to do is to get the HREF using Javascript and put in to a function.

function someFunction(hrefValue){
    // Run some functionallity here
}

I imagine that I have to, in my code-behind, add an onclick to my auto-generated anchor tags:

Controls.Add(new LitarelContro开发者_JS百科l("<a href='#"+ item.ID+"' onclick='someFunction();'>Hi</a>");

A friend of mine told me that I HAVE to specify which anchor tag's HREF I want, but as all of my anchor tags in my custom control runs the same Javascript, I can't specify one single HREF.

Any thoughts or suggestions?


Do pass this as parameter into someFunction();.

function someFunction(anchor){
    alert(anchor.getAttribute('href'));
}

Demo: http://www.jsfiddle.net/5HXK2/


You could modify the function (and the call) to pass in the object:

function someFunction(sender){     
   // get href from sender object
} 

Controls.Add(new LiteralControl("<a href='#"+ item.ID+"' 
        onclick='someFunction(this);'>Hi</a>");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜