我主要想知道在没有 PHP 的情况下这是否可行,如果可以的话也许需要一些指导。我感觉我对这个过程想得太多了。
目前,我可以通过单击按钮轻松地将特定的 .html 加载到 div 中。但是,我一直在尝试使用搜索栏将 .html 加载到 div 中(如标题明确指出的那样)。
例如,我搜索“Apple”并期望“Apple.html”加载到 div 中。我知道 JavaScript 不能/不应该在没有 PHP 帮助的情况下搜索本地文件,但是它不能“假设”该文件在那里并加载它吗?根据我的逻辑,这与单击按钮没有太大不同。
最佳答案
您可以使用jquery 您的所有文件应该位于实际页面存在的同一位置,否则您还必须指定路径。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>html</title>
<script src="http://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>
<input type="search" id="search">
<input type="button" id="button" name="">
<div>
</div>
<script type="text/javascript">
$("#button").on('click', function(e){
$('div').load($("#search").val() + ".html");
});
</script>
</body>
</html>
关于javascript - 使用搜索栏将 .html 加载到 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708630/