False通过onclick jQuery正确更改属性 [英] False True change attr by onclick jQuery

查看:72
本文介绍了False通过onclick jQuery正确更改属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过oncklick更改数据属性"aria-selected".
但是此脚本不起作用.你能帮我吗?

I need to change data attribute "aria-selected" by oncklick.
But this script does not work. Could you help me, please?

<a href="#" aria-selected="true" resource="">SHOW/HIDE</a>

这是我的代码:

<script>
$(document).ready(function($){
  $("a").attr("aria-selected","false");
  $(" ul li a").addClass("accordion");

  $('.accordion').click(function() {
    if ($(this).attr('aria-selected')) {
      $(this).attr("aria-selected","true");
    } 
    else {
      $(this).attr("aria-selected", "false");
    }
  });
});
</script>

推荐答案

aria-selected是字符串...不是布尔值.
因此,您必须将其与字符串进行比较.

The aria-selected is a string... Not a boolean.
So you have to compare it with a string.

<script>
$(document).ready(function($){
  $("a").attr("aria-selected","false");
  $(" ul li a").addClass("accordion");

  $('.accordion').click(function() {
    if ($(this).attr('aria-selected') == "false") {  // Change is here.
      $(this).attr("aria-selected","true");
    } 
    else {
      $(this).attr("aria-selected", "false");
    }
  });
});
</script>

这篇关于False通过onclick jQuery正确更改属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆