Javascript在变量中使用if语句
大家好,我的社区 我试图通过if语句和其他变量创建一个变量,但它不起作用。如果有人能帮助我,我将非常感激 这是我创建returntouser变量的代码Javascript在变量中使用if语句,javascript,variables,if-statement,Javascript,Variables,If Statement,大家好,我的社区 我试图通过if语句和其他变量创建一个变量,但它不起作用。如果有人能帮助我,我将非常感激 这是我创建returntouser变量的代码 function userinput() { return returntouser = metaTitle + "\n" + metaDescription + "\n" + metaKeywords; } 现在,我试图用“metaTitle”变量生成一个if函数。如果用户没有在textform中写入任何内容,“metaTitle”变量
function userinput() {
return returntouser = metaTitle + "\n" + metaDescription + "\n" + metaKeywords;
}
现在,我试图用“metaTitle”变量生成一个if函数。如果用户没有在textform中写入任何内容,“metaTitle”变量将不会添加到“returntouser”变量中
function userinput() {
return returntouser = if (document.getElementById("userinputTitle").length > 0) { return metaTitle } + "\n" + metaDescription + "\n" + metaKeywords;
}
有人能告诉我如何添加这个if函数,将“metaTitle”变量添加到“returntouser”变量中,或者不添加该变量吗?您必须在单独的步骤中进行操作
function userinput() {
var returntouser = "";
if (document.getElementById("userinputTitle").length > 0) { returntouser += metaTitle }
return returntouser + "\n" + metaDescription + "\n" + metaKeywords;
}
你必须分步做
function userinput() {
var returntouser = "";
if (document.getElementById("userinputTitle").length > 0) { returntouser += metaTitle }
return returntouser + "\n" + metaDescription + "\n" + metaKeywords;
}
您可以使用
?
-运算符执行条件内联。它也被称为三值运算符。更多信息
例如:
value = 10 > 3 ? "10 is bigger than 3" : "10 is smaller than 3";
您可以使用
?
-运算符执行条件内联。它也被称为三值运算符。更多信息
例如:
value = 10 > 3 ? "10 is bigger than 3" : "10 is smaller than 3";
尝试拆分您的操作:
function userinput() {
var meta = metaDescription + "\n" + metaKeywords;
if (document.getElementById("userinputTitle").length == 0) return meta;
return metaTitle + "\n" + meta;
}
或出于学习目的:
function userinput() {
var meta = [metaDescription, metaKeywords];
if (document.getElementById("userinputTitle")) meta.unshift(metaTitle);
return meta.join('\n');
}
或者在更现代的浏览器中
function userinput() {
var mt = document.getElementById('userinputTitle')? `${metaTitle}\n` : '';
return `${mt}${metaDescription}\n${metaKeywords}`;
}
尝试拆分您的操作:
function userinput() {
var meta = metaDescription + "\n" + metaKeywords;
if (document.getElementById("userinputTitle").length == 0) return meta;
return metaTitle + "\n" + meta;
}
或出于学习目的:
function userinput() {
var meta = [metaDescription, metaKeywords];
if (document.getElementById("userinputTitle")) meta.unshift(metaTitle);
return meta.join('\n');
}
或者在更现代的浏览器中
function userinput() {
var mt = document.getElementById('userinputTitle')? `${metaTitle}\n` : '';
return `${mt}${metaDescription}\n${metaKeywords}`;
}
if语句就是:一个语句。你不能用表达式来表达。您可以使用三元表达式,但为此,最好将整个if语句放在最后一行之前。对自由变量
returntouser
的赋值似乎很奇怪,也没有必要。您已经从函数返回了值。似乎您太努力了,无法将所有代码都放在一行中。您可以使用三元运算符?if语句的可能副本正是:一个语句。你不能用表达式来表达。您可以使用三元表达式,但为此,最好将整个if语句放在最后一行之前。对自由变量returntouser
的赋值似乎很奇怪,也没有必要。您已经从函数返回了值。似乎你太努力了,无法将所有代码都放在一行中。你可以使用三元运算符?可能重复变量名之前的字符串
原因?谢谢帮助:)为什么变量名之前的字符串
原因?谢谢帮助:)谢谢帮助:)谢谢帮助:)谢谢帮助:)我起初没有注意到模板字符串,以为你是在暗示三元运算符只在现代浏览器中可用。lol。模板现在也有点过时了,但只是在ES6中标准化了。ergonaut已经介绍了turnary,所以我不想麻烦了。谢谢你的帮助:)我起初没有注意到模板字符串,以为你是在暗示三元运算符只在现代浏览器中可用。lol。模板现在也有点过时了,但只是在ES6中才标准化。ergonaut已经覆盖了turnary,所以我不用麻烦了。