【问题标题】:Get Hex Values from Kuler API从 Kuler API 获取十六进制值
【发布时间】:2016-08-28 19:25:16
【问题描述】:

我正在尝试使用 JQ/JS 从 Adob​​e Kuler API 获取每个十六进制值:

kuler api xml

他们提供了一个 xml,但我很难抓住这些值并将它们放在这样的数组中:

var colors = [E6E2AF, A7A37E, EFECCA, 046380, 002F2F];

颜色存在于这个元素中

<description>
  Hex: E6E2AF, A7A37E, EFECCA, 046380, 002F2F
</description>

这里也是:

<kuler:swatchHexColor>
  E6E2AF
</kuler:swatchHexColor>

如果您有任何想法,我将不胜感激,

  • K

【问题讨论】:

    标签: javascript jquery xml api hex


    【解决方案1】:

    我花了一些时间找到一个适用于命名空间的选择器。这个选择器似乎可以完成这项工作。他在jQuery - XML parsing with namespaces 中的回答致谢Fasani

    $(xml).find('kuler\\:swatchHexColor, \\swatchHexColor');
    

    完整的sn-p:

    var colors = [];
    
    $.ajax({
        type: "GET",
        url: 'https://kuler-api.adobe.com/rss/get.cfm?listType=popular&itemsPerPage=5&key=mykey',
        dataType: "xml",
        success: function(data){
           // Select all <kuler:swatchHexColor> tags
           var colorHexs = $(data).find('kuler\\:swatchHexColor, \\swatchHexColor');
           
           // Loop through them, push them to colors array, and then append it body
           $(colorHexs).each(function(i, hex){
             
             colors.push($(hex).text()); // push to array
             $('body').append('<div class="color" style="background:#'+$(hex).text()+'">'+$(hex).text()+'</div>');
    
           });
    
           // colors output.
           console.log(colors);
          
          
        }
    });
    .color {
      width:33.334%;
      height:100px;
      float:left;
      text-align:center;
      line-height:100px;
    }
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"&gt;&lt;/script&gt;

    学分:

    【讨论】:

    • 非常感谢@Adam Azad,这正是我所需要的! :)
    猜你喜欢
    • 2015-08-01
    • 2014-02-18
    • 2020-02-12
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多