How to change link HREFs via javascript
I have a form where the user inputs an URL. The value resulting from that input after tweaking it a bit is the variable #url which is displayed to the user right away.
I need to make the like and tweet buttons in my site point to that URL. Making their HREFS equal to the variable #url
This are the share buttons:
<a id="twtbutton" href="http://twitter.com/share" class="twitter-share-button" d开发者_C百科ata-count="horizontal" data-via="theclocktwt">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>
<fb:like id="fbbutton" send="false" layout="button_count" width="450" show_faces="false" font="" style="position: absolute; left:110px;"></fb:like>
</div>
Here you can see how it works: http://www.chusmix.com/tests/magic.html
What appears as "Improved link" is the url I want the share buttons to use.
This is what you want to do -
var yourElement = document.getElementById('yourHref');
yourElement.setAttribute('href', '#url');
Reference the following page on the jquery site:
http://api.jquery.com/attr/
Should be able to use attr('href', 'value);
try this:
$("#url").keypress(
function(e) {
if(e.keyCode == 13) {
$("#output").attr('href', cleanURL($("#url").val()));
}
}
);
$('#twtbutton').prop('href', #url);
$('#fbbutton').prop('href', #url);
This would change the 'href' property of the html element(s) with an id=twtbutton and id=fbbutton to the value stored in #url, where the value stored in #url is a string datatype. This answer utilizes the jQuery library.
精彩评论