How might I replace a portion of a link's href attribute with a variable value?
I have the following code, written using jQuery:
var strval = '22'
$("#quic开发者_如何学Cknote").attr("href",
"javascript:location.href=/'http://site.com/notes/add?projects=/'+ strval")
Which results in the following attribute:
<a href="javascript:location.href='http://site.com/notes/add?projects='+'strval'"
id="quicknote">
But, what I want is:
<a href="javascript:location.href='http://site.com/notes/add?projects='+'22'"
id="quicknote">
Any jQuery wizards know how I might achieve this result?
Try:
var strval = "22";
$("#quicknote").attr("href",
"javascript:location.href='http://site.com/notes/add?projects=" + strval + "'");
Note the position and type of quotes.
On a side note, I'm not exactly sure why you wouldn't to do this instead:
var strval = "22";
$("#quicknote").attr("href",
"http://site.com/notes/add?projects=" + strval + "'");
ie theres no need for Javascript in your example.
Lastly, since you are using jQuery anyway I wouldn't put Javascript in the href like this either. Instead add a click()
handler:
$("#quicknote").click(function() {
window.location = "http://site.com/notes/add?projects=22";
return false;
});
A JQUERY wizard tells you, that it is possible, but it has just nothing to do with jQuery, and that your answer is:
You have the quote wrong!
And there is no reason to vote me down, because I answered the initial question in very, very, very professional style.
精彩评论