• <bdo id='NRrtM'></bdo><ul id='NRrtM'></ul>
    <tfoot id='NRrtM'></tfoot>
    <legend id='NRrtM'><style id='NRrtM'><dir id='NRrtM'><q id='NRrtM'></q></dir></style></legend>

    1. <small id='NRrtM'></small><noframes id='NRrtM'>

      <i id='NRrtM'><tr id='NRrtM'><dt id='NRrtM'><q id='NRrtM'><span id='NRrtM'><b id='NRrtM'><form id='NRrtM'><ins id='NRrtM'></ins><ul id='NRrtM'></ul><sub id='NRrtM'></sub></form><legend id='NRrtM'></legend><bdo id='NRrtM'><pre id='NRrtM'><center id='NRrtM'></center></pre></bdo></b><th id='NRrtM'></th></span></q></dt></tr></i><div id='NRrtM'><tfoot id='NRrtM'></tfoot><dl id='NRrtM'><fieldset id='NRrtM'></fieldset></dl></div>
      1. .appendChild() 不是使用 jQuery 时的函数

        时间:2023-09-06

        <tfoot id='cybwh'></tfoot>
          <tbody id='cybwh'></tbody>

            <i id='cybwh'><tr id='cybwh'><dt id='cybwh'><q id='cybwh'><span id='cybwh'><b id='cybwh'><form id='cybwh'><ins id='cybwh'></ins><ul id='cybwh'></ul><sub id='cybwh'></sub></form><legend id='cybwh'></legend><bdo id='cybwh'><pre id='cybwh'><center id='cybwh'></center></pre></bdo></b><th id='cybwh'></th></span></q></dt></tr></i><div id='cybwh'><tfoot id='cybwh'></tfoot><dl id='cybwh'><fieldset id='cybwh'></fieldset></dl></div>
            <legend id='cybwh'><style id='cybwh'><dir id='cybwh'><q id='cybwh'></q></dir></style></legend>

                • <bdo id='cybwh'></bdo><ul id='cybwh'></ul>

                  <small id='cybwh'></small><noframes id='cybwh'>

                • 本文介绍了.appendChild() 不是使用 jQuery 时的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从纯 JavaScript 过渡到 jQuery.我有一个 for 循环,它使用来自 API 的数据动态创建 HTML 元素.这是我的旧代码:

                  I am trying to transition from pure JavaScript to jQuery. I have a for loop that dynamically creates HTML elements with data from an API. Here is my old code:

                  recipeDiv = [];
                  recipeDiv[i] = document.createElement("div"); 
                  recipeDiv[i].setAttribute("class", "recipeBlock");
                  recipeDiv[i].appendChild(someElement);
                  

                  但是,当我转换到 jQuery 并改用它时

                  However, when I transitioned to jQuery and used this instead

                  recipeDiv = [];
                  recipeDiv[i] = $("<div/>").addClass("recipeBlock");
                  recipeDiv[i].appendChild(someElement);
                  

                  我收到以下错误:recipeDiv[i].appendChild 不是函数

                  我知道 .appendChild() 不是 jQuery (JS),但它不应该仍然有效吗?即使我使用 jQuery .append() 函数,我仍然会收到错误.

                  I know that .appendChild() isn't jQuery (JS), but shouldn't it still work? Even if I use the jQuery .append() function, I still get an error.

                  非常感谢任何帮助.

                  推荐答案

                  您似乎对互换 jQuery 和 DOM API 感到困惑.它们不能互换使用.document.createElement返回一个 Element$("<div/>") 返回jQuery 对象.Element 对象有 appendChild 方法,jQuery 对象有 append 方法.

                  You seem to be confusing yourself by inter-changing jQuery and DOM APIs. They cannot be used interchangeably. document.createElement returns an Element and $("<div />") returns the jQuery object. Element object has the appendChild method and jQuery object has the append method.

                  作为一种好的做法,我建议您在 DOM API 或 jQuery 之间进行选择,并坚持下去.这是针对您的问题的纯基于 jQuery 的解决方案

                  As a good practice, I would suggest you choose between DOM APIs or jQuery, and stick to it. Here is a pure jQuery based solution to your problem

                  var recipeContainer = $("<div/>")
                    .addClass("recipeContainer")
                    .appendTo("body");
                  
                  var recipeDiv = [];
                  var likes = [];
                  
                  for (var i = 0; i < 20; i++) {
                  
                    //Create divs so you get a div for each recipe
                    recipeDiv[i] = $("<div/>").addClass("recipeBlock");
                  
                    //Create divs to contain number of likes
                    likes[i] = $("<div/>")
                      .addClass("likes")
                      .html("<b>Likes</b>");
                  
                    //Append likes blocks to recipe blocks
                    recipeDiv[i].append(likes[i]);
                  
                    //Append recipe blocks to container
                    recipeContainer.append(recipeDiv[i]);
                  }

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

                  这篇关于.appendChild() 不是使用 jQuery 时的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:自定义 rc-time-picker 的样式 下一篇:React.js 中的 setState 与 replaceState

                  相关文章

                • <small id='tDfEL'></small><noframes id='tDfEL'>

                      <bdo id='tDfEL'></bdo><ul id='tDfEL'></ul>

                    <i id='tDfEL'><tr id='tDfEL'><dt id='tDfEL'><q id='tDfEL'><span id='tDfEL'><b id='tDfEL'><form id='tDfEL'><ins id='tDfEL'></ins><ul id='tDfEL'></ul><sub id='tDfEL'></sub></form><legend id='tDfEL'></legend><bdo id='tDfEL'><pre id='tDfEL'><center id='tDfEL'></center></pre></bdo></b><th id='tDfEL'></th></span></q></dt></tr></i><div id='tDfEL'><tfoot id='tDfEL'></tfoot><dl id='tDfEL'><fieldset id='tDfEL'></fieldset></dl></div>
                    <tfoot id='tDfEL'></tfoot>
                    <legend id='tDfEL'><style id='tDfEL'><dir id='tDfEL'><q id='tDfEL'></q></dir></style></legend>