开发者

Links in javascript

开发者 https://www.devze.com 2023-02-17 16:54 出处:网络
I have some dynamically created links inmy pages and what I need to do is when I click on the link, I should pass the name of the link to another page. So, please show me开发者_运维问答 a way to accom

I have some dynamically created links in my pages and what I need to do is when I click on the link, I should pass the name of the link to another page. So, please show me开发者_运维问答 a way to accomplish this. Thanx in advance :)


If you are using jquery, you can use :

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<a href="#foo">foo</a> <a href="#bar">bar</a>
<script>
  $('a').click(function () { alert($(this).text()); /* attr('href') */ });
</script>
</html>

Of course the html an so are not good, but the click thing is what you need. Then you could use ajax, or a parameter in the target URL to give the link to the other page.

But why don't you generate that when creating links? I mean add a href="mylink?from=mylink".

Edit: corrected with your comment. What you need is text() rather than attr('href').


document.getElementById('anchor').innerHTML;

then you can pass this value as a parameter, to another page.


documetn.getElementById("mylink").setAttribute("href", "newlink");
documetn.getElementById("mylink").InnerHTML = "new link name";


var link = document.getElementById('link');
link.setAttribute("href", link.getAttribute("href") + "?linkName=" + encodeURI(link.innerHTML));

This will make your link be something like this:

<a id="link" href="http://somewebsite.com?linkName=Name_of_the_link">Name_of_the_link</a>

Then on the "other" page you can access the link name through the GET variable linkName.

0

精彩评论

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

关注公众号