简体   繁体   English

在JS问题中重构jQuery

[英]Refactoring jQuery in JS issues

I use jQuery frequently but I have realized that I need to understand the underlying Javascript better. 我经常使用jQuery,但我意识到我需要更好地了解底层Javascript。 I have worked out the AJAX part but am stuck on the nested functions. 我已经解决了AJAX部分,但停留在嵌套函数上。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

document.onreadystatechange = function() {
  var state = document.readyState;

  if (state == 'interactive') {
    $('#button').on("click", function() {
      $.getJSON(apiurl, function(json) {

        $.each(json.photos.photo, function(i, myresult) {
          apiurl_size = "https//api/myresult";
          $.getJSON(apiurl_size, function(size) {
            $.each(size.sizes.size, function(i, myresult_size) {
              if (myresult_size.width == selected_size) {
                $("#results").append('<p class="col-md-4"><a href="" ><img src="' + myresult_size.source + '"/></a></p>');
              }
            })
          })
        });
      });
    });
  } else if (state == 'complete') {
    document.getElementById('complete');
  }
}

What I want to know: 我想知道的是:

  1. how to grab the id selector in $('button') since document.getElementById('button') returns the entire element but I cannot enact the .on click functionalitiy 如何获取$('button')的id选择器,因为document.getElementById('button')返回整个元素,但是我无法制定.on click功能

  2. breaking the .getJSON function out into a pure JS reusable function .getJSON函数分解为纯JS可重用函数

  1. $("#button").on("click", cb_function) is analogous to document.getElementById("button").addEventListener("click", cb_function) . $("#button").on("click", cb_function)类似于document.getElementById("button").addEventListener("click", cb_function) If you were using a selector that can match multiple elements, it's similar except that it loops over all the returned elements to add event listeners to all of them. 如果使用的选择器可以匹配多个元素,则除了将循环返回的所有元素都添加到事件监听器之外,其他选择器均类似。

  2. $.getJSON(URL, cb_function) creates an AJAX request to the URL whose onreadystatechange callback function essentially does: $.getJSON(URL, cb_function)创建对该URL的AJAX请求,该URL的onreadystatechange回调函数基本上可以执行以下操作:

    1. call JSON.parse() on the response. 在响应上调用JSON.parse()
    2. Call cb_function with the result of the parse. 调用cb_function及其解析结果。

A simple version would be like: 一个简单的版本是这样的:

function getJSON(url, cb_function) {
    var xhr = new XMLHTTPRequest();
    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        if (xhr.readystate == 4 && xhr.status == 200) {
            cb_function(JSON.parse(xhr.responseText));
        }
    }
    xhr.send();
    return xhr;
}

The real $.getJSON allows you to supply parameters to the URL, and it automatically converts from an object to the appropriate URL-encoded string to add as URL parameters. 真实的$.getJSON允许您向URL提供参数,并且它会自动从对象转换为适当的URL编码的字符串以添加为URL参数。 But neither of your calls has data parameters, so this definition should work for you. 但是您的调用都没有数据参数,因此此定义应该对您有用。

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

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