variable insertion in href attribute of <a> tag
I have a variable with this value
var= "http://www.coolsite.com"
this variable will change, its a dynamic value
i want insert this variable in my href attribute of tag.
after insertion it should look like th开发者_如何学编程is
<a href="http://www.coolsite.com">http://www.coolsite.com</a>
i want to do this in asp.net c#
does any one have an idea, how can i do this?
Thanks
In the markup, this can be achieved with the following:
<asp:HyperLink ID="HyperLink1" NavigateUrl="http://www.coolsite.com" runat="server">http://www.coolsite.com</asp:HyperLink>
Notice the NavigateUrl attribute. This is the URL that will be placed inside of the href. The inner text is the text that is rendered to the client. Knowing this, you can achieve the same results with this code in your code behind:
string yourUrl = "http://www.coolsite.com";
this.HyperLink1.NavigateUrl = yourUrl;
this.HyperLink1.Text = yourUrl;
If you variable is on the server side use an asp:Hyperlink instead and set it when the value gets changed.
If the var="http://www.coolsite.com" is a javascript variable. you could use javascript to modify the href attribute of a anchor tag. If you use jQuery you can use the attr method.
$('Atag').attr({href:"http://www.coolsite.com"});
If you use plain js code, you can use:
co = document.getelementById('AtagID');
co.setAttribute('href',URL);
<a id="theLink">http://www.coolsite.com</a>
so to code in jquery:
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$("#theLink").attr("href", "http://www.coolsite.com");
});
</script>
精彩评论