如何在ARIA-menuitemcheckbox(javascript)中检查(选择)元素?

时间:2017-04-16 10:15:57

标签: javascript jquery html browser-automation

我有以下HTML项目(menubox):

<div class="uiContextualLayerPositioner uiLayer" data-testid="undefined" data-ownerid="u_0_10" style="width: 473px; left: 288px; top: 290px;">
   <div class="uiContextualLayer _5v-0 _53il uiContextualLayerBelowLeft">
      <div class="_54nq _57di _558b _2n_z" id="u_0_y">
         <div class="_54ng">
            <ul class="_54nf" role="menu">
               <li class="_54ni _5ipo __MenuItem" role="presentation">
                  <a class="_54nc _54nu" href="#" role="menuitemcheckbox">
                  <span>
                  <i class="mrs img sp_Kw8-3yVLtZo sx_864bd8"></i>
                  <span class="_54nh">Item1</span>
                  </span>
                  </a>
               </li>
               <li class="_54ni _5ipo _54nd __MenuItem" role="presentation">
                  <a class="_54nc _54nu" href="#" data-testid="notif_setting_higlights" role="menuitemcheckbox" aria-checked="true">
                  <span>
                  <i class="mrs img sp_Kw8-3yVLtZo sx_864bd8">
                  </i>
                  <span class="_54nh">Item2</span>
                  </span>
                  </a>
               </li>
               <li class="_54ni _5ipo __MenuItem" role="presentation">
                  <a class="_54nc _54nu" href="#" role="menuitemcheckbox">
                  <span>
                  <i class="mrs img sp_Kw8-3yVLtZo sx_864bd8"></i>
                  <span class="_54nh">Item3</span>
                  </span>
                  </a>
               </li>
               <li class="_54ni _5ipo __MenuItem" role="presentation">
                  <a class="_54nc _54nu" href="#" role="menuitemcheckbox">
                  <span>
                  <i class="mrs img sp_ey5BbZl6hUH sx_780712">
                  </i>
                  <span class="_54nh">Item4</span>
                  </span>
                  </a>
               </li>
            </ul>
         </div>
      </div>
   </div>
</div>

我可以看到取消选择了Item1,并选择了Item2:

aria-checked="true"

我想取消选择Item2并选择Item1。为此,从Item1中获取表示已选择

的objectID
_54nc _54nu

并致电

document.getElementById("_54nc _54nu").setAttribute("aria-checked", true)', 'about: blank', 0)

但没有发生。我错过了什么?

P.S。我看到所有类的对象ID都是相同的,但我也尝试了其他id。也许我误解了班级的id问题,也许是它的不同之处。请指点我。 :)

1 个答案:

答案 0 :(得分:1)

document.getElementById()不正确,因为您的元素都没有id 您想使用document.getElementsByClassName()