如何获得<a> element and use it somewhere else in the same html page

时间:2019-01-24 10:24:33

标签: javascript c# jquery html

I'm building a website and I'm trying to get the title of a link, when clicking it and pass it to somewhere else in the same page.

I've tried using JavaScript to do this but I don't have very good knowledge of it so I'm not even sure if this is possible.

<a class="album" href='/<%=vars.getSessionValue("SelectedCulture").ToString() + "/menu/" + menuID%>/galeria.aspx?id=<%=gallery.ID %>' title="<%=new stringHelper().simpleClean(gallery.title) %>"></a>

It's a photo gallery built in ASP.NET so the links are being generated by a foreach loop that gets the links and titles from our backoffice, using the c# code behind. When you click a certain link, you're redirected to that specific album. The problem is that it gets different titles, and I need the specific title attr of the album I click, and then use that title somewhere else on the same page. It loads the content of that album into the same html page and I need the title of that album.

1 个答案:

答案 0 :(得分:0)

如果您使用jQuery,则可以使用$(".album")来获取相册类中的任何元素。

没有jQuery,您可以使用document.getElementsByClassName("album")

然后使用您的元素,如果使用jQuery,则可以调用.attr("title");如果使用纯JS,则可以调用.title

示例

$(".album").attr("title")

document.getElementsByClassName("album")[0].title