load an image after clicking a button

前端 未结 3 1634
一向
一向 2020-12-20 09:34

I have a

相关标签:
3条回答
  • 2020-12-20 10:20

    HTML

    <button data-rel="example.jpg">Click Me</button>
    

    jQuery

    $("button").click(function () {
        var imgUrl = $(this).data('rel');
        $("#area").html("<img src='" + imgUrl + "' alt='description' />");
    });
    
    0 讨论(0)
  • 2020-12-20 10:22

    Change your code to this:

    $(document).ready(function(){
    $("button").click(function(){
        var imgUrl = $(this).data('rel');
        $("#area").html("<img src='" + imgUrl + "' alt='description' />");
      });
    });
    
    <button data-rel="example.jpg">Click Me</button>
    <div id="area"></div>
    

    Fiddle: http://jsfiddle.net/x3QdU/4/

    0 讨论(0)
  • 2020-12-20 10:24

    change

    <button rel=
    

    to

    <button data-rel=
    

    http://jsfiddle.net/x3QdU/1/

    0 讨论(0)
提交回复
热议问题