overflow-wrap不会破坏动态添加的div上的单词

时间:2017-12-31 21:02:47

标签: javascript jquery html css ajax

配方的成分从API Ajax调用动态添加到div。每行文本都保留在一行中,这意味着它不会破坏并包裹在下面的行中。我尝试通过CSS样式类添加word-break和overflow-wrap,然后动态添加类。我没有变化。顺便说一下,我的子弹点也不会出现在每个配料线的前面。

我在我的主要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>

1 个答案:

答案 0 :(得分:2)

使用li {white-space: normal;}作为列表项。

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