问题
I can't figure this one out...
Could someone please help me en explain how it works to?
Goal: Adding a variable to a hard-coded URL, and put the new string into a href
Example:
var x = "1000";
var y = "http://www.google.com"
var result = y + x;
<a href="! the value of result !">click here...</a>
Edit i'm sorry i meant y + x ofcourse...
回答1:
Given this HTML code:
<a href="" target="_blank">click here...</a>
You would need this bit of js code: (working example http://jsfiddle.net/QUEZL)
var x = "http://www.google.com/"
var y = "#q=test";
var result = x + y;
$('a').attr('href', result);
回答2:
Try
$("a").attr("href", result)
回答3:
HREF is an attribute so you can access it via .attr()
var x = "1000";
var y = "http://www.google.com/"
var result = y + x;
$('a').attr({'href':result});
回答4:
Give your Link an ID.
<a id="lnkTarget" href="#">click here...</a>
Add a reference to JQuery JavaScript Library.
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
Get a handle on the link with a JQuery Selector and use the attr function.
$('#lnkTarget').attr('href', result);
回答5:
JavaScript:
document.getElementsByTagName('a')[0].href = result ;
jQuery:
$("a").attr("href", result)
回答6:
For me, all the above approaches were replacing the variable with the latest value of the variable as it was referred in a loop.
Solution:
$.each(statusForUI, function( key, value ) {
x = "http://192.168.10.100:8888/workflow/" + key;
$('.listTable').append(
'<tr><td>' +
'<a href="' + x + '"> env_name </a>' +
key +
'</ td></tr>'
);
});
来源:https://stackoverflow.com/questions/20334601/jquery-appending-variable-to-url-and-use-it-in-a-href