Adding a link to an image [closed]
开发者_运维问答
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this questionIs there a way to add a link to an image using an input box, let say you have an image uploaded and you have an input,
Image Link: <input type="text" id="image_link"/>
in this box a user types in www.google.com and you want the link to append to the image. Is there a way to do this?
Did you mean something like this: http://jsfiddle.net/2QRBm/ ?
HTML:
<input/>
<button>set link</button>
<img src="http://media.heavy.com/post_assets/2010/03/0417/1267743226_stoned-party-dog-original.jpg" width="300"/>
Javascript:
$('img').wrap($('<a/>',{href:'',target:'_blank'}));
$('button').click(function(){
var val=$('input').val();
$('img').closest('a').attr('href',val.replace(/^([^(http://)].*)/,'http://$1'));
});
Lets say your image have this id: #img
Then you can use jQuery wrap
to wrap the image into a
a element:
$('input').change(function(){
$('#img').wrap('<a href="' + $(this).val() + '"></a>')
})
DOCS: http://api.jquery.com/wrap/
Something like this: live demo
HTML:
<input type="text" id="test"/> <input type="button" id="go" value="go"/>
JavaScript:
$("#go").click(function(){
var value = $("#test").val();
window.location = "http://"+value+"";
});
精彩评论