Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何访问jQuery JSON中的HTML元素属性_Javascript_Jquery_Html_Json - Fatal编程技术网

Javascript 如何访问jQuery JSON中的HTML元素属性

Javascript 如何访问jQuery JSON中的HTML元素属性,javascript,jquery,html,json,Javascript,Jquery,Html,Json,我试图访问特定的HTML元素属性并将其分配给JSON属性 首先,我从文件中获取JSON对象并将其加载到设置中。然后我遍历这些行,创建具有各种属性的文本输入 因为我使用的是虹膜插件,所以我会马上启动它。您可以看到,我正在使用changeElements函数,其中使用了iris id(有效) 所以问题是。。。为什么虹膜部分中的color属性为空 function startCorr(jsonFile) { request = new XMLHttpRequest(); request

我试图访问特定的HTML元素属性并将其分配给JSON属性

首先,我从文件中获取JSON对象并将其加载到设置中。然后我遍历这些行,创建具有各种属性的文本输入

因为我使用的是虹膜插件,所以我会马上启动它。您可以看到,我正在使用changeElements函数,其中使用了iris id(有效)

所以问题是。。。为什么虹膜部分中的color属性为空

function startCorr(jsonFile) {
    request = new XMLHttpRequest();
    request.open('GET', jsonFile, true);
    request.onload = function() {
        if (request.status >= 200 && request.status < 400) {
            settings = JSON.parse(request.responseText);
            $.each(settings, function(key, jsonRow) {
                $(sidePanel).append(createInput(key, jsonRow));
            });
            // iris
            $('.iris').iris({
                color: $(this).attr("iris-color"), // doesn't work
                width: 200,
                border: false,
                hide: false,
                change: function(event, ui) {
                    changeElements($(this).attr("iris-id"), ui);
                }
            });
        } else {
            console.log("Error getting JSON file");
        }
    };
    request.send();
}

function createInput(key, jsonRow) {
    input  = "<label>" + jsonRow.name + "<input type='text' class='iris' id='" + jsonRow.section + "' ";
    input += "iris-color='" + getColor(jsonRow.selectors[0]) + "' iris-id='" + key + "'>";
    input += "</label>"

    return input;
}

function getColor(selectorObject) {
    return $(selectorObject.selector).css(selectorObject.style);
}

当您需要访问特定于元素的数据以传递到插件的选项中时,一种非常常见的方法是在
$中初始化插件。每个
循环。在循环
中,该
是当前元素

$('.iris').each(function() {
  var $el = $(this);
  $el.iris({
    color: $el.attr("iris-color"), 
    width: 200,
    border: false,
    hide: false,
    change: function(event, ui) {
      changeElements($el.attr("iris-id"), ui);
    }
  });
});

当您需要访问特定于元素的数据以传递到插件的选项中时,一种非常常见的方法是在
$中初始化插件。每个
循环。在循环
中,该
是当前元素

$('.iris').each(function() {
  var $el = $(this);
  $el.iris({
    color: $el.attr("iris-color"), 
    width: 200,
    border: false,
    hide: false,
    change: function(event, ui) {
      changeElements($el.attr("iris-id"), ui);
    }
  });
});