【发布时间】:2017-10-11 04:16:48
【问题描述】:
所以我正在努力更新我的一个编码游乐场,但遇到了一些麻烦。
基本上,如果用户ONLY 在散列中有“结果”字符串,就像这样......
testhash.html#d81441bc3488494beef1ff548bbff6c2?result
我仅希望它显示结果 ('only result was "result") 而没有其他内容,但如果用户在字符串中有编辑器,我希望它拆分并显示编辑器或在本例中文字...
testhash.html#d81441bc3488494beef1ff548bbff6c2?md,html,css,js,result
但是,我很难弄清楚如何过滤它,如果它只显示哈希中的结果字符串以忽略其他 if 语句。
如果函数只显示哈希中的“结果”字符串,我如何过滤到哪里让它忽略其他 if 语句而只显示结果?
var url = window.location.hash,
testhash = "#d81441bc3488494beef1ff548bbff6c2",
arr = ["md", "html", "css", "js", "edit", "dark", "transparent"];
$(document).ready(function(e) {
if (!url) {
window.location.href = location.href + testhash + "?md,html,css,js,result";
} else {
// Show Editors If URL Contains Them
if (url.indexOf("?") > -1) {
if (url.indexOf("result") > -1) {
if ($.inArray("result", arr) > -1) {
$(document.body).append('only result was "result" 1');
return false;
}
if (url.indexOf("md") > -1) {
$(document.body).append('md');
}
if (url.indexOf("html") > -1) {
$(document.body).append('html');
}
if (url.indexOf("css") > -1) {
$(document.body).append('css');
}
if (url.indexOf("js") > -1) {
$(document.body).append('js');
}
if (url.indexOf("dark") > -1) {
$(document.body).append('dark');
}
if (url.indexOf("transparent") > -1) {
$(document.body).append('transparent');
}
$(document.body).append('result');
}
}
}
});
【问题讨论】:
-
如果它在哈希 url 中,您只想显示结果吗?而不是进入其他 if 语句,那么解决方案是将所有 if 语句包装在 if (url.indexOf("result") > -1) {} 的 else {} 中,就像这样 if (url.indexOf("? ") > -1) { if (url.indexOf("result") > -1) { ... } else { if (url.indexOf("md") > -1) { $(document.body).附加('md'); } if (url.indexOf("html") > -1) { $(document.body).append('html'); } ...} }
-
如果显示“ONLY”结果,我想要文本
only result was "result"。但是如果其他字符串在有或没有结果的情况下可见,我希望它附加文本,如代码示例中所示
标签: javascript jquery arrays if-statement hash