这实际上是 this question 的后续问题 感谢 Rory McCrossan 解决了这个问题。 我现在有了这个功能脚本;一个搜索函数,根据搜索词显示一个 div。
JS
$('#search').click(function() {
var txt = $('#search-criteria').val();
if (txt)
$('.fruit').hide().filter('#' + txt.toLowerCase()).show();
});
CSS
.fruit {
display: none;
}
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script
<input type="text" id="search-criteria" />
<input type="button" id="search" value="search" />
<div class="fruit" id="apple">
<h3>Some text about apples</h3>
</div>
<div class="fruit" id="orange">
<h3>Some text about oranges</h3>
</div>
我现在想知道是否有人可以帮助我将某种错误处理添加到此脚本中,最好可以顺利添加而无需重写脚本逻辑。 IE。当搜索没有结果和/或用户进行空字符串搜索时,我想显示另一个带有消息的 div。
由于我实际上是一名用户体验设计师,所以我的技术技能有些有限,因此如果有人可以帮助我解决这个问题,我将非常感激......
提前致谢!
最佳答案
使用 try 和 catch 进行简单的 javascipt 错误处理:--
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function adddlert2($a){
alert($a);
}
try {
adddlert("Welcome guest!");
}
catch(err) {
document.getElementById("demo").innerHTML = err.message;
}
</script>
</body>
</html>
关于javascript - Javascript 搜索函数中的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47386355/