开发者

How to include javascript generated text into html link?

I have a javascript that displays a generated text in开发者_JAVA技巧to a div: document.getElementById('phrase').innerHTML = phrase; PHRASE_TEXT_GETS_SHOWN_HERE

Basically I'm trying to set up a link that will take the text and post it to twitter with a link: Clicky for tweety

How can I include the generated text in the link?


Do you mean like:

function setPhrase(phrase) {
  var url = 'http://twitter.com/home?status=' + encode(phrase);
  $('#phrase').html('<a href="' + url + '">' + phrase + '</a>');
}

...?

Un-jQuerying it should be straightforward enough, if that's how you roll.

This is un-jQueried:

function setPhrase(phrase) {
  var url = 'http://twitter.com/home?status=' + encodeURI(phrase);
  document.getElementById('phrase').innerHTML = '<a href="' + url + '">' + phrase + '</a>';
}

If you didn't see my failbraining encode for encodeURI as an error you should use Firebug. It may also fail if you have more than one element with the id phrase or if you have no elements with the id phrase.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜