简体   繁体   English

jQuery 中有没有办法修改 .html() 以便添加代码而不是覆盖?

[英]Is there a way in jQuery to modify .html() so that it adds code rather than overwriting?

I'm doing work to retrieve data from APIs.我正在努力从 API 检索数据。 I'm designing my code to make multiple queries to the API and then adding them to the running list of results in my HTML.我正在设计我的代码来对 API 进行多次查询,然后将它们添加到我的 HTML 中的结果运行列表中。 Due to the data returned by the API, there isn't a way to get it all in one shot and then slice it up, I need to get it in multiple shots.由于 API 返回的数据,没有办法一次获取所有内容然后将其切片,我需要多次获取它。

Look at this code:看看这段代码:

function renderResults(results) {
    return $('#results-section').html(genResults(results));
}

The function renderResults() is being called inside my API call函数renderResults()在我的 API 调用中被调用

.fetch(URL)
   .then([code that is working properly])
   .then(call renderResults() here)

I've console logged my API results and tested the API call in Postman so I know it's constructed correctly- just trying to give proper context.我已经控制台记录了我的 API 结果并在 Postman 中测试了 API 调用,所以我知道它的构造正确 - 只是试图提供适当的上下文。

To review, results is the data from the API, results-section is how the results container in my HTML is marked, and the function genResults(results) creates the template HTML with values from results stuck into it.回顾一下, results是来自 API 的数据, results-section是我的 HTML 中的结果容器的标记方式,函数genResults(results)创建模板 HTML,其中包含results值。

When you use .html it overwrites everything in the section indicated immediately prior.当您使用.html它会覆盖之前指示的部分中的所有内容。 I'm looking for a way to add the template HTML generated by genResults(results) to any HTML already in the container I have ID'd results-section , not replace what's there.我正在寻找一种方法将genResults(results)生成的模板 HTML 添加到容器中已有的任何 HTML 中,我有 ID results-section ,而不是替换那里的内容。 I've tried chaining on .append() but can't seem to find a way to do so successfully.我试过链接.append()但似乎找不到成功的方法。

The obvious solution is to have a for loop create as many results sub-sections as I need, but the problem is that I don't know how many sections I need until I get the results from the API.显而易见的解决方案是让 for 循环根据需要创建尽可能多的结果子部分,但问题是在从 API 获得结果之前,我不知道需要多少部分。

Any ideas?有任何想法吗?

Update:更新:

Answer from Souvik Ghosh worked. Souvik Ghosh 的回答有效。 I was trying to chain .append() on to .html() , I didn't realize it actually replaces it.我试图将.append().html() ,我没有意识到它实际上取代了它。

$( "#results-section" ).append( "Your data from the last API call" );

You could use jQuery append.您可以使用 jQuery 追加。

$( "#results-section" ).append( "Your data from the last API call" );

Read more: https://api.jquery.com/append/阅读更多: https : //api.jquery.com/append/

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

相关问题 jQuery`.after()`呈现HTML而不是解析HTML - JQuery `.after()` Renders HTML Rather Than Parsing It jQuery before()输出为字符串而不是HTML - jQuery before() outputting as string rather than HTML Javascript可以修改HTML流而不是DOM树吗? - Can Javascript modify the HTML stream rather than DOM tree? 编写通过JavaScript而非纯HTML解析的链接的最佳方法是什么? (jQuery选项卡) - What's the best way to write links that resolve by JavaScript rather than pure HTML? (jQuery tabs) 使用jquery检索原始html而不是呈现的html - Retrieving the original html, rather than the rendered html, with jquery 谁能解释为什么这段代码连接而不是添加数值? - Can anyone explain why this code concatenates rather than adds the numerical values? jQuery追加方法插入文本而不是HTML - jQuery append method inserting text rather than HTML jquery ajax正在加载整个外部html而不是目标div - jquery ajax is loading entire external html rather than targeted div 谷歌地图“ pin”可以是HTML代码而不是图像吗? - Can a google maps “pin” be HTML code rather than an image? 是否可以将Template7代码放在单独的文件中而不是html中 - Is that possible to put Template7 code in a separate file rather than in html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM