Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中嵌入的HTML中创建if语句以显示CSS类_Javascript_Jquery - Fatal编程技术网

如何在JavaScript中嵌入的HTML中创建if语句以显示CSS类

如何在JavaScript中嵌入的HTML中创建if语句以显示CSS类,javascript,jquery,Javascript,Jquery,如何根据js数组中是否有数据显示两个类中的一个 例如,如果有响应数据。标题显示灰色类,否则显示白色类 我现在正在尝试: let resultHTML = '<article class="item">\ <div class="post-main">\ <header class="clear">\ <a hre

如何根据js数组中是否有数据显示两个类中的一个

例如,如果有响应数据。标题显示灰色类,否则显示白色类

我现在正在尝试:

let resultHTML = '<article class="item">\
                    <div class="post-main">\
                        <header class="clear">\
                            <a href="' + responseData.url + '"</a>\
                            <span class="post-date">just now</span>\
                        </header>\
                        'if (responseData.title) {
                            '<section class="grey">'
                        } else {
                            '<section class="white">'
                        }'\
                            ' + articleImage + '\
                            <h3>' + responseData.title + '</h3>\
                            <div class="post-about">' + responseData.about + '</div>\
                        </section>\
                    </div>\
                </article>';
let resultHTML='0\
\
\
您可以使用。
在您的情况下,它将是这样的:

let resultHTML='0\
\
\
我更喜欢用更干净的

演示:

let responseData={title:'abc'}
让articleImage='test';
让resultHTML=`

忍者速度。基本上:条件:结果是否为真?如果结果为假;谢谢,现在将进行测试。@Uby像这样,我得到的不仅仅是整个解决方案,而是基于@Uby answer将三元数括在()中。对我有用。