Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在使用mysite.com/?search=words idea返回和第四次访问搜索结果页面时,如何让搜索结果页面保持在原位?_Javascript_Html_Css - Fatal编程技术网

Javascript 在使用mysite.com/?search=words idea返回和第四次访问搜索结果页面时,如何让搜索结果页面保持在原位?

Javascript 在使用mysite.com/?search=words idea返回和第四次访问搜索结果页面时,如何让搜索结果页面保持在原位?,javascript,html,css,Javascript,Html,Css,当我在浏览器中单击“后退”按钮或“刷新/重新加载”按钮时,我的搜索/过滤结果消失,即使“输入框”保持正常并记住键入的关键字。我如何让结果页面在导航回第四个页面或使用mysite.com/?search=words刷新/重新加载时保留关键字 JS HTML 搜寻 提示:键入“标题1”、“标题2”、“标题3”。。。 类型 类型1 标题 标题1 描述 说明1 日期 日期1 类型 类型2 标题 标题2 描述 说明2 日期 日期2 类型 类型3 标题 标题3 描述 说明3 日期

当我在浏览器中单击“后退”按钮或“刷新/重新加载”按钮时,我的搜索/过滤结果消失,即使“输入框”保持正常并记住键入的关键字。我如何让结果页面在导航回第四个页面或使用mysite.com/?search=words刷新/重新加载时保留关键字

JS

HTML


搜寻

提示:键入“标题1”、“标题2”、“标题3”。。。






类型 类型1 标题 标题1 描述 说明1 日期 日期1

类型 类型2 标题 标题2 描述 说明2 日期 日期2

类型 类型3 标题 标题3 描述 说明3 日期 日期3





您可能可以将搜索值保存在会话存储中,并在重新访问页面时应用它们

const params = window.location.search;
sessionStorage.setItem('previous_search', params);

下次访问该页面时,请检查url参数是否为空,然后从会话存储中获取它们。

从我如何阅读您希望执行的操作中,我成功地使用了该方法。它在用户的浏览器中添加了一个历史事件,这样他们可以像往常一样来回导航,并且可以访问您自己添加的伪事件。然后添加一个历史侦听器,这样当您来回/重新加载时,它可以看到该历史点,并使用其中的数据触发任何您想要的函数

查看您的代码,看起来您的输入没有提交按钮,但是您可以从任何常规JS事件触发pushState事件。我将添加一些示例代码,以便您可以看到一种可能性

例如:

let input=document.getElementById('myInput'),
文本='';
//输入字段上的键控侦听器
$(输入).on('keyup',函数(e){
text=input.value;
//将文本更改添加到浏览器历史记录中,以便用户可以返回/前进,并在历史记录状态下保存url参数
让历史状态={
“文本”:文本,
}
pushState(historyState,null,`?search=${text}`);
});
//在历史记录侦听器发现搜索文本存在后运行某些操作
const updateSearch=函数(文本){
log(`I发现搜索为:${text}`);
}
//历史监听器
$(窗口).on('popstate',函数(e){
让state=e.originalEvent.state
if(state!==null){//如果文本存在
updateSearch(state.text)
}else{//如果没有文本集(URL没有参数)
console.log('未找到历史记录状态')
}
});
。输入换行{
边缘底部:12px;
}
#myInput:无效~。提示{
显示:块;
}
#诺马奇人:空的,
#noMatches:空+提示{
显示:无;
}
.1 tr{
显示:无;
}
.1.展示{
显示:表格行;
}
#myTable表tr:第一个子td标记{
背景:橙色;
字体大小:粗体;
颜色:黑色;
}
标记{
背景:初始;
}
.style1{
文本对齐:左对齐;
}

搜寻

提示:键入“标题1”、“标题2”、“标题3”。。。






类型 类型1 标题 标题1 描述 说明1 日期 日期1

类型 类型2 标题 标题2 描述 说明2 日期 日期2

类型 类型3 标题 标题3 描述 说明3 日期 日期3





请提供一些示例代码Xtanks和我确实喜欢这种简单的方法,但到目前为止,这些方法都不起作用-该死!这个snippet@couchwoodMy snippet遇到的问题更多地说明了我如何使用history.pushState()方法记录输入历史,并能够在浏览器中返回/前进。在您自己的代码中实现该方法时,您遇到了什么困难?即使只是实现第一行代码,也会阻止筛选的页面显示任何内容,这并不重要。historySession是我想要的方式。@couchwood是否更简单?“即使只实现第一行代码,也会阻止筛选页面显示任何内容”-这是否涉及变量声明?如果是这样,您可能使用的JS版本不能使用let,因此您需要使用vars。除此之外,我还将查看API页面上的示例,以查看一个更简单的示例:
.input-wrap  {
  margin-bottom: 12px;
}

#myInput:invalid ~ .hints {
  display: block;
}



#noMatches:empty, #noMatches:empty + .hints {
  display: none;
}


.style1 tr {
  display: none;
}


.style1 .show {
  display: table-row;
}



#myTable table tr:first-child td mark {
background: orange;
font-weight: bold;
color: black;
}
mark {
background: initial;
}    .style1  {
text-align: left;
}
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js">
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1
/mark.min.js"></script>
<head>
<body>
<div class="input-wrap">
<label>
Search 
<input id="myInput" type="text" required
   placeholder="Search Titles" />
</label>
</div>

<div class="hintsWrap">
<p id="noMatches"></p>
<p class="hints">
Hints: type "Title1", "Title2", "Title3"...
</p>
</div>
<br />
<br />
<br />
<table id="myTable" style="width: 100%" class="style1">
    <tr>
        <td>
<br />
<br />
<table style="width: 100%">
    <tr>
        <td>
        <table style="width: 100%">
            <tr>
                <th class="style1">Type</th>
                <td>type1</td>
            </tr>
            <tr>
                <th class="style1">Title</th>
                <td>title1</td>
            </tr>
            <tr>
                <th class="style1">Description</th>
                <td>description1</td>
            </tr>
            <tr>
                <th class="style1">Date</th>
                <td>date1</td>
            </tr>
        </table>
        </td>
    </tr>
</table>
<br />
<br />
        <table style="width: 100%">
            <tr>
                <th class="style1">Type</th>
                <td>type2</td>
            </tr>
            <tr>
                <th class="style1">Title</th>
                <td>title2</td>
            </tr>
            <tr>
                <th class="style1">Description</th>
                <td>description2</td>
            </tr>
            <tr>
                <th class="style1">Date</th>
                <td>date2</td>
            </tr>
        </table>
<br />
<br />
        <table style="width: 100%">
            <tr>
                <th class="style1">Type</th>
                <td>type3</td>
            </tr>
            <tr>
                <th class="style1">Title</th>
                <td>title3</td>
            </tr>
            <tr>
                <th class="style1">Description</th>
                <td>description3</td>
            </tr>
            <tr>
                <th class="style1">Date</th>
                <td>date3</td>
            </tr>
        </table>
<br />
<br />
<br />
<br />
<br />
        </td>
    </tr>
</table>
const params = window.location.search;
sessionStorage.setItem('previous_search', params);