如果隐藏的div值等于XML中的属性,则追加到类

时间:2014-09-09 10:19:58

标签: javascript jquery html xml

我现在已经搜索并试了两天,没有任何运气。

我在尝试:

我通过xslt中的foreach生成了几个div,它包含一个隐藏的div,我存储一个代码来匹配外部xml文件的值。像这样:

<div class="pakket_content">
    <a href="http://">
        <div class="waardering_wrapper col-xs-12">
            <div class="sterwaardering col-xs-8">
                <img src="http://">
            </div>
            <div class="CODE">CODE</div>
            <div class="waardering col-xs-4">
                <p>-</p>
            </div>
            <div class="waardering pull-right">
                <b>-</b>
            </div>
        </div>
    </a>
    <a href="">
        <button type="button" class="btn btn-default btn-md pull-right col-xs-12">
            Nu boeken!
            <span class="glyphicon glyphicon-arrow-right"></span>
        </button>
    </a>
</div>

div类&#39;代码&#39;包含要匹配的代码。

出来的xml是:

<entities>
    <accommodation cms-id="458245" external-id="CODE" name="Trip A">
        <destination cms-id="45541" name="Paramaribo" level="destination"/>
        <country cms-id="4545" name="Suriname" level="country"/>
        <accommodation-type>Hotel</accommodation-type>
        <testimonial-count>88</testimonial-count>
        <average-testimonial-score>7.6</average-testimonial-score>
        <deep-link>
            http://link.com
        </deep-link>
        <testimonial-count-per-language>88</testimonial-count-per-language>
        <testimonial-count-all>88</testimonial-count-all>
        <average-testimonial-score-all>7.6</average-testimonial-score-all>
    </accommodation>
</entities>

现在我想将平均证言分数附加到div&#34; waardering&#34;当external-id属性等于in中的值时。但是我该怎么做呢?

我尝试循环遍历div类和属性的值,如下所示:

$(document).ready(function() {

    $.ajax({
        type: "GET",
        url: "file.xml",
        dataType: "xml",
        success: function(xml) {

            $(xml).find('accommodation').each(function(index) {
                var cijfer = $(this).find('average-testimonial-score-all').text();

                $('.CODE').each(function(index) {

                    var divcode = $(this).text();

                    $.attr('external-id').each(function(index) {
                        var attrcode = $(this).text();

                        if (divcode == attrcode) {
                            $(".waardering").append(cijfer);
                        };
                    });
                });
            });
        }
    });
});

没有结果。 有人能用这个推动我朝着正确的方向前进吗?

1 个答案:

答案 0 :(得分:0)

  • 以正确的方式提取accommodation external-id(根据how .attr()应该有效),代码可以正常工作。

  • 我省略了Ajax请求测试。

Fiddle

$(document).ready(function()
{
    var xml = '<entities><accommodation cms-id="458245" external-id="CODE" name="Trip A"> <destination cms-id="45541" name="Paramaribo" level="destination"/> <country cms-id="4545" name="Suriname" level="country"/>  <accommodation-type>Hotel</accommodation-type> <testimonial-count>88</testimonial-count> <average-testimonial-score>7.6</average-testimonial-score> <deep-link>http://link.com</deep-link> <testimonial-count-per-language>88</testimonial-count-per-language> <testimonial-count-all>88</testimonial-count-all> <average-testimonial-score-all>7.6</average-testimonial-score-all> </accommodation></entities>';

    $(xml).find('accommodation').each(function(index)
    {
        var cijfer = $(this).find('average-testimonial-score-all').text();
        var externalId = $(this).attr("external-id");
        $('.CODE').each(function(index)
        {
            var divcode = $(this).text();
            if (divcode == externalId)
            {
                $(".waardering").append(cijfer);
            };
        });
    });
});