【发布时间】:2021-03-25 23:18:17
【问题描述】:
我想隐藏一些给定条件的元素。我只是希望它们不要出现在 html 中。我试过display: none。但是好像没有效果
我在想这样的事情:
let beliefs_add_content = function(num_bins, tokens, divid, results = 0, show_submit = 1) {
let inhtml = ""
if (results == 1) {
inhtml += "<div id=\"results_text\">\n";
if (show_submit == 1) {
inhtml += "<br>\n";
}
inhtml += "<br>\n";
inhtml += "</div>\n";
}
if (results == 1; display: 'none' ) {
inhtml += "<div id=\"top_div\">\n";
inhtml += "<div id=\"question_div\"></div>\n";
inhtml += "<div id=\"chart_wapper\">\n";
inhtml += "<div id=\"bar_div\">\n";
inhtml += "<div id=\"axis_div\"></div>\n";
for (let i = 1 ; i <= num_bins ; i++) {
inhtml += "<div class=\"bar_amt\" id=\"bar_amt" + i + "\">0</div>\n";
inhtml += "<div class=\"div_bar\" id=\"bar" + i + "\"> </div>\n";
}
inhtml += "</div>\n";
inhtml += "</div>\n";
inhtml += "<div id=\"label_div\">\n";
for (let i = 1 ; i <= num_bins ; i++) {
inhtml += "<div class=\"label_val\" id=\"label" + i + "\" ></div>\n";
}
inhtml += "</div>\n";
inhtml += "<div id=\"unallocated\">" + tokens + "</div>\n";
inhtml += "<div id=\"prob_div\">\n";
for (let i = 1 ; i <= num_bins ; i++) {
inhtml += "<div class=\"prob_val\" id=\"prob" + i + "\" ></div>\n";
}
inhtml += "</div>\n";
inhtml += "<div id=\"amount_div\">\n";
for (let i = 1 ; i <= num_bins ; i++) {
inhtml += "<div class=\"amt_val\" id=\"amt" + i + "\" ></div>\n";
}
inhtml += "</div>\n";
inhtml += "<div class=\"slider_div\">\n";
for (let i = 1 ; i <= num_bins ; i++) {
inhtml += "\t<div class=\"cell_val\" id=\"cell" + i + "\">\n";
inhtml += "\t\t<input type=\"range\" name=\"bin" + i + "\" id=\"bin" + i + "\" class=\"form-control slider\" value=\"0\" min=\"0\" max=\"" + tokens + "\" required/>\n";
inhtml += "\t</div>\n";
}
inhtml += "</div>\n";
inhtml += "<!-- Field for the alternative labels -->\n";
inhtml += "<input type=\"hidden\" name=\"labelset\" id=\"labelset\" value=0 class=\"form-control\" required />\n";
inhtml += "<div class=\"submit_div\">\n";
inhtml += "<div id=\"submit_message\"> </div>\n";
inhtml += "<table id=\"submit_table\"><tr>\n";
inhtml += "<td style=\"text-align:left;\">\n";
if (beliefs_ns.beldat["bin_button"] != "") {
inhtml += "<button class=\"btn btn-primary btn-large\" type=\"button\" id=\"alt_button\">Alternate Labels</button>\n";
}
inhtml += "</td>\n";
inhtml += "<td style=\"text-align:right;\">\n";
// We don't show the submit button on the final page
if (show_submit == 1 & results == 0) {
let onclick = (results == 0 ? "onclick=\"beliefs_ns.submit_click();\"" : "");
inhtml += "<button class=\"btn btn-primary btn-large\" type=\"\" id=\"submit_button\" " + onclick + ">Submit</button>\n";
}
inhtml += "</td>\n";
inhtml += "</tr></table>\n";
inhtml += "</div>\n";
inhtml += "</div>\n";
$("#" + divid).html(inhtml);
}
}
但它运行得不是很好。有什么建议么?我需要发布其他代码吗?
【问题讨论】:
-
这条线应该做什么?
if (results == 1; display: 'none' ) { -
在上一页给出用户选择的结果后,我不想显示所有用于做出选择的东西。
-
这不是 javascript 中的有效语法。如果要将元素的样式设置为
none,则需要使用document.querySelector("YOUR_ELEMENT").style.display = "none" -
您能否详细说明“它运行得不是很好”?
-
这个
display: 'none'是不对的...在一个类似的条件下...
标签: javascript element show-hide