Javascript 使用特定哈希调用URL时展开details元素
我使用了Javascript 使用特定哈希调用URL时展开details元素,javascript,html,hashchange,Javascript,Html,Hashchange,我使用了details和summary元素来折叠页面的一部分 显示评论 此 是 酷 这是一个放松哈希检查的问题,允许不仅仅是“注释”,还允许“注释-111”。我假设,正如您的HTML所示,您的ID是数字的 //grab hash and its parts let hash = location.hash.match(/^#(comment(s|-(\d+)))$/); if (hash) { //resolve to element let el = document.g
details
和summary
元素来折叠页面的一部分
显示评论
此
是
酷
这是一个放松哈希检查的问题,允许不仅仅是“注释”,还允许“注释-111”。我假设,正如您的HTML所示,您的ID是数字的
//grab hash and its parts
let hash = location.hash.match(/^#(comment(s|-(\d+)))$/);
if (hash) {
//resolve to element
let el = document.getElementById(hash[1]);
if (el) {
//open comments - not sure what your .open property does
el.closest('#comments').open = true;
//if is hash to specific comment, go to it
if (hash[3]) location.href = '#'+hash[3];
}
}
隐马尔可夫模型。。。不知怎的,我错过了细节元素。。。但问题是,您可以选择散列中id为的元素,如果该元素存在,则查找detail元素并将其open属性设置为true。
if(hash.indexOf('comment')!=-1)var details=document.getElementById(hash.closest('details')代码>@Banzay谢谢!这在扩展#注释中起作用,但是页面在#注释-X元素的位置没有像我想的那样移动#注释在加载DOM时还没有扩展..?我假设页面上有更多的详细信息
元素,为什么不使用getElementById
或querySelector
查找“哈希”元素呢。。?