开发者

Adding a link to an image [closed]

开发者 https://www.devze.com 2023-04-09 03:55 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. 开发者_运维问答
Closed. This question needs details or clarity. It is not currently accepting answers.
开发者_运维问答

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

Is 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+"";
 });
0

精彩评论

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