简体   繁体   中英

Display preview onkey with textarea

Can anybody help me? I want to to show a preview of an image link in . If you enter the link in a (prefix .png or .jpg for example) it should be displayed in the img element. please no php or server-side at all, it should display images/links like http://hellopledge.files.wordpress.com/2012/07/hello_medium-file3.jpg

Thank you

Like this: http://jsfiddle.net/uHnFp/

$('#field').keyup(function(){
   $('#img img').attr('src',$(this).val());
});

Modified:

http://jsfiddle.net/uHnFp/6/

$('#field').keyup(function(){
    var text = $(this).val();
    var url = text.match(/https?:\/\/(?:[a-z\-]+\.)+[a-z]{2,6}(?:\/[^\/#?]+)+\.(?:jpe?g|gif|png)/g);
    var i = 0;
    var html = '';
    while(i < url.length){
        html += '<img src="'+url[i]+'" />';
        console.log(url);
        i++;
    }
    if(url.length > 0){
        $('#img').fadeIn();
        $('#img').html(html);
    } else {
        $('#img').fadeOut();   
    }    
});

This will only get the first image. This could get more complex. You could make it preview each image it finds. Multiple images, etc. I haven't tested if this regex is exhaustive just one I found online.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM