javascript - 展开和折叠按钮javascript

标签 javascript html

我正在研究展开和折叠按钮。我这里有两张图片,焦点应该放在元素上。如果单击一次,它应该折叠,再单击一次应该展开。我尝试聚焦元素,但 .focus() 不起作用。

HTML:

<td  aria-expanded="false" name="td_tohide2" id="td_tohide2" style="padding:0px;" class="sectiontd" nowrap="true" >
  <img  role="button" aria-labelledby="hide_show" id="generalinfo_caretup" src="images/arrow_up_ps_oc.png" onkeypress="sh_keypress('td_tohide2','td_toshow2','generaltable',event)" onclick="sh('td_tohide2','td_toshow2','generaltable')" tabindex="0" />
</td>

<td aria-expanded="true" aria-labelledby="hide_show_" name="td_toshow2" id="td_toshow2" style="padding:0px;display:none" class="sectiontd" nowrap="true">
  <img role="button" id="generalinfo_caretdown" src="images/arrow_down_ps_oc.png" onkeypress="sh_keypress('td_toshow2','td_tohide2','generaltable',event)" onclick="sh('td_toshow2','td_tohide2','generaltable')" tabindex="0" />
</td>

Javascript:

function sh_keypress(a,b,c,event){
  if( event.keyCode==13 || event.keyCode==32 || event.which==13 || event.which==32) {  
    if(document.getElementById(c).style.display=="none")
      document.getElementById(c).style.display = "";
    else
      document.getElementById(c).style.display = "none";
      document.getElementById(a).style.display="none"; //arrow images
      document.getElementById(b).style.display=""; //arrow images
      setTimeout("document.getElementById(b).focus()",1000);
      event.stopImmediatePropagation();  
    }   
  }
function sh(a,b,c)
  {

if(document.getElementById(c).style.display=="none")
  document.getElementById(c).style.display = "";
else
  document.getElementById(c).style.display = "none";


document.getElementById(a).style.display="none"; //arrow images
document.getElementById(b).style.display=""; //arrow images
}

任何人都可以告诉我如何聚焦元素。

最佳答案

什么是sh?您尚未提供具有该名称的函数。假设已提供该函数并遵循相同的模式,您将需要确保将 b 的值传递到 setTimeout

当前 setTimeoutb 的值很可能未定义。这是因为 setTimeout 默认在全局上下文中执行。相反,您可以这样做:

function sh_keypress(a,b,c,event) {
    if(event.keyCode==13 || event.keyCode==32 || event.which==13 || event.which==32) {  
        if(document.getElementById(c).style.display === 'none') {
            document.getElementById(c).style.display = "";
        } else {
            document.getElementById(c).style.display = "none";
            document.getElementById(a).style.display="none"; //arrow images
            document.getElementById(b).style.display=""; //arrow images
            // Use a function below to provide the value of `b` from `sh_keypress` scope
            setTimeout(function(){document.getElementById(b).focus()},1000);
            event.stopImmediatePropagation();
        }
    }
}

关于javascript - 展开和折叠按钮javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39285253/

相关文章:

javascript - 如何使用 javascript 编写小图像弹出窗口?

javascript - 我需要调整 Fabric js 中所选项目的大小

javascript - 导出并使 MYSQL 数据库中的字符串有效期为 7 天

jquery - 在一定的浏览器大小后删除模态背景(jasny bootstrap)

javascript - VBA点击悬停下拉菜单

javascript - CSS Tab 控件 - 如何只显示第一个 div?

javascript - 当变体的色样悬停在 Shopify 上时,如何显示变体图像?

javascript - D3js高亮栏一一连续

html - 为什么 flex 元素不缩小过去的内容大小?

html - 当按钮为 "active"时,我想将所有图像制作为 "active"的图片