Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.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 使用特定哈希调用URL时展开details元素_Javascript_Html_Hashchange - Fatal编程技术网

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
    查找“哈希”元素呢。。?