简体   繁体   English

DOM未根据GET请求和数据属性更新

[英]DOM not updating on GET request + data-attributes

Issue: Upon updating the src of images, retrieved via GET request, the DOM never updates but their new values show in console. 问题:更新通过GET请求检索的图像的src后,DOM从不更新,但其新值会显示在控制台中。

Suspected Cause: I think there is some conflict with using data-attributes, but using attr() instead of data() does not seem to remedy. 可疑原因:我认为使用数据属性存在一些冲突,但是使用attr()代替data()似乎无法解决。

HTML to be updated: 要更新的HTML:

<div class="data-block">
    <img data-item="hp-logo" />
    <img data-item="hp-banner" />
</div>

GET Request: GET请求:

if(promoid != null) {       
    $.get({
        url: '/snippets/data.html',
        cache: false
    }).then(function(data){
        var tempData = $('<output>').append($.parseHTML(data)).find('.data[data-promo-id="' + promoid + '"]');
        myContent = tempData.html();
        dataItems = $('.data-block').html();
        //console.log('Data Items On Page: ', dataItems);
        $(dataItems).each(function (index, value) {
            if( $(this).is('[data-item]')) {
                //console.log('Data Items With Attribute: ', this);
                dataItemLookup = $(this).attr('data-item');
                //console.log('Data Item Lookup Value: ', dataItemLookup);
                $(myContent).each(function (index, value) {
                    //console.log('Retrieved Items Checking Against: ', this);
                    if ( $(this).attr('data-alias') == lastalias ) {
                        //console.log('Retrieved Items Match Alias: ', this);
                        if ($(this).attr('data-item') == dataItemLookup) {
                            //console.log('Retrieved Item Match', this);
                            dataImageDesktop = $(this).attr('data-image-desktop');
                            //console.log('Value to be Passed to Data Item: ', dataImageDesktop);
                        } else {
                            // Do nothing
                        }
                    } else {
                        // Do nothing
                    }
                });
                $(this).attr('src', dataImageDesktop);
                console.log(this);
            }
        });
    }); 
}

data.html: data.html:

<div class="data-container">
    <div class="data" data-promo-id="11202016">
        <div data-alias="test.html" data-item="hp-logo" data-image-desktop="http://placehold.it/250x150"></div>
        <div data-alias="test.html" data-item="hp-banner" data-image-desktop="http://placehold.it/350x250"></div>
        <div data-alias="not-test.html" data-item="hp-spot" data-image-desktop="http://placehold.it/450x350"></div>
    </div>
</div>

Not sure how to proceed in troubleshooting this issue. 不知道如何解决此问题。 Everything works as expected, except the DOM updating. 除DOM更新外,一切都按预期工作。 Ideas? 想法?

Using html() on an element will get you the innerHTML of the object, which is a string. 在元素上使用html()将为您获取对象的innerHTML,它是一个字符串。 As such using it inside $() later will cause jQuery to create new elements that are not attached to the DOM. 因此,稍后在$()中使用它会导致jQuery创建未附加到DOM的新元素。 If all you are after is to select elements and modify them, simply use the $(selector) and modify it. 如果只需要选择元素并进行修改,则只需使用$(selector)进行修改。 Do not use html() and wrap the results with $(). 不要使用html()并用$()包装结果。

代替$(selector).attr('data-name')尝试使用$(selector).data('name') ,如jQuery.data()文档中所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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