Updating the value of data attribute using jQuery

后端 未结 4 2333
长情又很酷
长情又很酷 2020-12-08 18:38

I have the following HTML code:


    \"No\"

        
相关标签:
4条回答
  • 2020-12-08 18:50
    $('.toggle img').data('block', 'something').attr('src', 'something.jpg');
    
    0 讨论(0)
  • 2020-12-08 19:01
    $('.toggle img').data('block', 'something');
    $('.toggle img').attr('src', 'something.jpg');
    

    Use jQuery.data and jQuery.attr.

    I'm showing them to you separately for the sake of understanding.

    0 讨论(0)
  • 2020-12-08 19:06
    $('.toggle img').each(function(index) { 
        if($(this).attr('data-id') == '4')
        {
            $(this).attr('data-block', 'something');
            $(this).attr('src', 'something.jpg');
        }
    });
    

    or

    $('.toggle img[data-id="4"]').attr('data-block', 'something');
    $('.toggle img[data-id="4"]').attr('src', 'something.jpg');
    
    0 讨论(0)
  • 2020-12-08 19:08

    I want to change the width and height of a div. data attributes did not change it. Instead I use:

    var size = $("#theme_photo_size").val().split("x");
    $("#imageupload_img").width(size[0]);
    $("#imageupload_img").attr("data-width", size[0]);
    $("#imageupload_img").height(size[1]);
    $("#imageupload_img").attr("data-height", size[1]);
    

    be careful:

    $("#imageupload_img").data("height", size[1]); //did not work
    

    did not set it

    $("#imageupload_img").attr("data-height", size[1]); // yes it worked!
    

    this has set it.

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