javascript - 引号内的 If...Else 语句

标签 javascript if-statement quotes

我想在下面的字符串中合并 if else 语句。当我尝试时,我在浏览器控制台中收到 Uncaught SyntaxError: Unexpected token if 。我该怎么办...

        for(var i=0; i<articles.length; i++) {

            var $article = "<div class='col-xs-12 col-sm-6 col-md-3 col-lg-3 fix-box-height'>" +
                "<a class='shadow-hovering' href="+"'"+articles[i].full_url+"'"+">" +
                "<div class='thumbnail color-after-hover'> " +
                "<img class='thumbnail-img' src="+"'"+ articles[i].thumbnail_url +"'"+" data-src='3' alt=''>" +
                "<div class='caption box-read-more'>"+
                    if(articles[i].title.length > 28){
                        "<h4>"+"'"+ articles[i].title.substr(0, 28) +"'..."+"</h4>"
                    } else if (articles[i].title.length > 16) {
                        "<h4>"+"'"+ articles[i].title +"'"+"</h4>"
                    } else {
                        "<h4>"+"'"+ articles[i].title +"'"+"</h4> <br>"
                    }
                    if(articles[i].subtitle.length > 37){
                        "<h4>"+"'"+ articles[i].subtitle.substr(0, 37) +"'..."+"</h4>"
                    } else if (articles[i].subtitle.length > 15) {
                        "<h4>"+"'"+ articles[i].subtitle +"'"+"</h4>"
                    } else {
                        "<h4>"+"'"+ articles[i].subtitle +"'"+"</h4> <br><br>"
                    }
                    +"<p> <span class='btn btn-primary more-box-button'>More</span> </p> " +
                    "</div>" +
                    "</div>" +
                    "</a>" +
                    "</div>";
        }

最佳答案

尝试:

    for(var i=0; i<articles.length; i++) {

        var $article = "<div class='col-xs-12 col-sm-6 col-md-3 col-lg-3 fix-box-height'>" +
            "<a class='shadow-hovering' href="+"'"+articles[i].full_url+"'"+">" +
            "<div class='thumbnail color-after-hover'> " +
            "<img class='thumbnail-img' src="+"'"+ articles[i].thumbnail_url +"'"+" data-src='3' alt=''>" +
            "<div class='caption box-read-more'>";
                if(articles[i].title.length > 28){
                    $article += "<h4>"+"'"+ articles[i].title.substr(0, 28) +"'..."+"</h4>"
                } else if (articles[i].title.length > 16) {
                    $article += "<h4>"+"'"+ articles[i].title +"'"+"</h4>"
                } else {
                    $article += "<h4>"+"'"+ articles[i].title +"'"+"</h4> <br>"
                }
                if(articles[i].subtitle.length > 37){
                    $article += "<h4>"+"'"+ articles[i].subtitle.substr(0, 37) +"'..."+"</h4>"
                } else if (articles[i].subtitle.length > 15) {
                    $article += "<h4>"+"'"+ articles[i].subtitle +"'"+"</h4>"
                } else {
                    $article += "<h4>"+"'"+ articles[i].subtitle +"'"+"</h4> <br><br>"
                }
                $article += "<p> <span class='btn btn-primary more-box-button'>More</span> </p> " +
                "</div>" +
                "</div>" +
                "</a>" +
                "</div>";
    }

关于javascript - 引号内的 If...Else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909863/

相关文章:

javascript - 与多层数组匹配字符串

javascript - 为什么我的图标没有显示?

javascript - 访问新窗口 - cypress.io

python - Python "If...elif...else"循环 1,500 个选项的替代技术

javascript - 如何单独显示/隐藏具有相同类名的div?

java - “如果”和 'Else' 在 Java 中不起作用?

regex - 单引号内的单引号问题 - Bash 命令行 GNU 并行

windows - 带有变量的目录中的 mysqldump 空间

javascript - 一行中多个引号

powershell - 在 PowerShell 的 Active Directory 中测试计算机对象是否存在