gpt4 book ai didi

javascript - 模板文字函数内的 If 语句

转载 作者:行者123 更新时间:2023-12-02 22:33:45 24 4
gpt4 key购买 nike

我浏览了很多有关此问题的其他帖子,但我似乎找不到适合我的特定问题的答案。基本上我有一个显示卡片的“应用程序”页面。每张卡都是一个单独的应用程序。这些卡片是使用函数内的模板文字从 JavaScript 对象生成的。我有三个独立的对象,一个用于 EngineeringOfficeIn House

我需要做什么?

现在,当您加载网站时,您只会看到三张代表类别的卡片。我想根据用户想要的类别从不同的对象生成项目。例如:当用户单击Engineering卡上的按钮时,只会生成approved_Engineering_Software对象内的项目。

我遇到的问题是我在模板文字函数中使用 if 语句 时遇到问题。我希望能够做这样的事情:

function categoryTemplate(card) {
if (`${card.title}` == "Engineering"){
return `
<card class="nested">
<img class="image" src="${card.icon}">
<div class="text">
<h3>${card.title}</h3>
<p>${card.description}</p>
<button class="request-button" onclick="show_Engineering_items()">Request</button>
</div>
</card>
`;
}
}

我已经检查了 here herehere,但这些链接中给出的答案都没有成功起作用。

如果您需要更多上下文,这是我的代码:

HTML

<!DOCTYPE html>
<html>
<head>
<title>Software Request</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>

<body class="main-page">
<div id="category-view"></div>
<div id="app"></div>
<script src="script.js"></script>
</body>
</html>

JavaScript

const main_Category_View = [
{
title: "Engineering",
description: "From Autocad to something else that I will write in later, all your Enginerring Software is found here",
icon: "Assets/docker1.png"
},
{
title: "Office",
description: "General purpose office applications e.g Microsoft Office, Excel, Powerpoint, Visio etc",
icon: "Assets/docker1.png"
},
{
title: "In House",
description: "Apps build by In House specifically for In House",
icon: "Assets/docker1.png"
}
];

const approved_Engineering_Software = [
{
title: "Microsoft Azure",
description: "Microsoft Azure is a cloud computing service created by Microsoft for building, testing, deploying, and managing\
applications and services through Microsoft-managed data centers.",
icon: "Assets/azure.png"
},
{
title: "Visual Studio Code",
description: "Visual Studio Code is a source-code editor developed by Microsoft for Windows, Linux and macOS. It includes support \
for debugging, embedded Git control and GitHub, and code refactoring.",
icon: "Assets/vscode.png"
},
];

const approved_Office_Software = [
{
title: "Microsoft Word",
description: "Microsoft Word is a word processor developed by Microsoft. It was first released on October 25, 1983 under the name Multi-Tool Word for Xenix systems.",
icon: "Assets/word.png"
}
];

//sorts everything perfectly
var sortByProperty = function (property) {
return function (x, y) {
//basically returns nothing if they are equal,
//or returns the item that is greater than the lesser
return ((x[property] === y[property]) ? 0 : ((x[property] > y[property]) ? 1 : -1));
};
};

// completes the rest of the structure of the cards
function softTemplate(item) {
return `
<card class="nested">
<img class="image" src="${item.icon}">
<div class="text">
<!--<button class="request-button" onclick="getDescription('${item.description}')">Request</button> -->
<h3>${item.title}</h3>
<p>${item.description}</p>
<button class="request-button" onclick="getDesc()">Request</button>
</div>
</card>
`;
}

function categoryTemplate(card) {
return `
<card class="nested">
<img class="image" src="${card.icon}">
<div class="text">
<!--<button class="request-button" onclick="getDescription('${card.description}')">Request</button> -->
<h3>${card.title}</h3>
<p>${card.description}</p>
<button class="request-button" onclick="show_items()">Request</button>
</div>
</card>
`;
}

document.getElementById("category-view").innerHTML = `
<div class="stack">
<main class="grid">
${main_Category_View.map(categoryTemplate).join("")}
</main>
</div>
`;

let sorted_Office_List = approved_Office_Software.sort(sortByProperty('title'));
let sorted_Engineering_List = approved_Engineering_Software.sort(sortByProperty('title'));

// Sets up the structure of the page and sends our Template Literal to two functions for work.
function show_items() {
document.getElementById("app").innerHTML = `
<div class="stack">
<main class="grid">
${sorted_Office_List.map(getDesc).join("")}
${sorted_Office_List.map(softTemplate).join("")}
</main>
</div>
`;
}

function show_Engineering_items() {
document.getElementById("app").innerHTML = `
<div class="stack">
<main class="grid">
${sorted_Office_List.map(softTemplate).join("")}
</main>
</div>
`;
}

最佳答案

模板文字将被翻译为字符串。你有:

if (`${card.description} == "Engineering"`)

将被翻译为:

if('Engineering == "Engineering"')

非空字符串为真;你想做的可能是:

if (card.description === "Engineering")

关于javascript - 模板文字函数内的 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58807657/

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