gpt4 book ai didi

javascript - 溢出包装不会破坏动态添加的 div 上的文字

转载 作者:行者123 更新时间:2023-11-30 14:51:28 25 4
gpt4 key购买 nike

食谱的成分通过 API Ajax 调用动态添加到 div。每行文本保留在一行中,这意味着它不会中断并换行到下面的行。我已经尝试通过 CSS 样式类添加分词符和溢出换行,然后动态添加类。我没有零钱。顺便说一句,我的要点也不会出现在每个成分行的前面。

我确实在我的主 CSS 样式表上方附加并链接了一个 CSS 重置样式表。

https://imgur.com/a/i0ZJc - 截图

JavaScript

$( document ).ready(function(){

function recipeSearch(foodtype){
var searchTerm = $(this).attr("data-name");
var queryURL = "https://api.edamam.com/search?q=" + foodtype + "&app_id=c13467eb&app_key=9cd5b15a4c49f23093c8fe1f1d2c7ce4"

$.ajax({
url: queryURL,
method: "GET"
})
.done(function(response) {
console.log(response);


for (var k = 0; k<10; k++){
var foodDiv = $("<div class = 'recipeCard'>");


if (response.hits.length === 0) {
$("#recipeOutput").append("<p>" + "Please enter valid recipe ingredient or name." + "</p>");
};

var recipeName = response.hits[k].recipe.label;
var pOne = $("<p>").html("<h2>" + recipeName + "</h2>");
foodDiv.append(pOne);

var recipePic = response.hits[k].recipe.image;
var pTwo = $("<img class='recipeImage'>").attr("src", recipePic);
foodDiv.append(pTwo);

var recipeURL = response.hits[k].recipe.shareAs;
var pThree =$("<p>").html("<a target='_blank' href='" + recipeURL + "'>" + "Get full recipe here" + "</a>");
foodDiv.append(pThree);

var length = response.hits[k].recipe.ingredients.length;

var ingList = $("<ul>");
for (var i = 0; i < length; i++){

var ing = response.hits[k].recipe.ingredients[i].text;
var pFour = $("<li class='loopText wordBreak'>").text(ing);
ingList.append(pFour);
};
foodDiv.append(ingList);

var health = response.hits[k].recipe.healthLabels;
var pFive = $("<p>").text(health);
foodDiv.append(pFive);

$("#recipeOutput").append(foodDiv);
}

});
};


function clear(){
$("#recipeOutput").empty();
}

$("#recipeSearch").on("click", function(event){
var foodtype = $("#recipeInput").val().trim();
clear();
recipeSearch(foodtype);
return false;
});
});

$("#recipeInput").keyup(function(event) {
if (event.keyCode === 13) {
$("#recipeSearch").click();
}
});

CSS

li.loopText {
display: block;
overflow-wrap: break-all;
padding: 2px;
margin 1px auto;
list-style-type: circle;
}
label {
color: white;
}
wordBreak {
overflow-wrap: break-word;
}

HTML

        <div class="col s12 m12 l6 xl6">
<div class=" panel-boxes">
<div class="panel-heading">WHAT WOULD YOU LIKE TO COOK?</div>
<div class="panel-body">
<div class="form-group">
<input type="text" class="form-control" id="recipeInput">
<button type="search" class="btn" id="recipeSearch" style="margin-right: 20px; margin-left: 15px">Search</button>
</div>
<div class="panel-body recipesContainer" id="recipeOutput" ></div>
</div>
</div>
</div>

最佳答案

对列表项使用 li {white-space: normal;}

这似乎适用于 Chrome 和 Safari。或者,对整个 ul 使用 ul {white-space: normal;},所有列表项都将继承它。

关于javascript - 溢出包装不会破坏动态添加的 div 上的文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48045639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com