jquery .is(':disabled') 不工作

jquery .is(':disabled') not working

如果 liattr disabled=true,我想添加 class 禁用。但不知何故,这是行不通的。请看看有没有人可以帮忙。

$('.expandable-panel').find('.expandable').each(function () {
     if(!$(this).is(':disabled')) {
       var title = $(this).find('.expand-title');
       $(title).click(function () {
         $(this).parent().toggleClass('expanded');
       });
     }else {
       $(this).addClass('disabled');
     }
    });
.expandable-panel {
  padding: 0;
  margin: 0;
  list-style: none;
}
li.disabled {background: red;}
.expandable-panel > li {
  border: 1px solid #c7d0d8;
  border-radius: 3px;
  background: #ffffff;
  margin-bottom: 10px;
}
.expandable-panel > li .expand-title {
  background: none;
  font-weight: normal;
  width: 100%;
  text-align: left;
  position: relative;
  padding-right: 36px;
}
.expandable-panel > li .expand-title:after {
  position: absolute;
  right: 16px;
  top: 14px;
  border: 5px transparent solid;
  content: ' ';
  border-top-color: #669BC6;
}
.expandable-panel > li:hover {
  border-color: #99A6B5;
}
.expandable-panel > li:hover .expand-title:after {
  border-top-color: #136FD5;
}
.expandable-panel > li .panel-content {
  visibility: hidden;
  opacity: 0;
  height: 0;
  transition: all 0.3s ease;
}
.expandable-panel > li .panel-content > .content {
  padding: 1.2em;
}
.expandable-panel .expanded {
  border: 1px solid #99A6B5;
}
.expandable-panel .expanded .panel-content {
  visibility: visible;
  opacity: 1;
  height: auto;
}
.expandable-panel .expanded .expand-title {
  border-bottom: 1px solid #99A6B5;
  border-radius: 3px 3px 0 0;
}
.expandable-panel .expanded .expand-title:after {
  top: 8px;
  border: 5px transparent solid;
  content: ' ';
  border-bottom-color: #669BC6;
}
.expandable-panel .expanded:hover .expand-title:after {
  border-top-color: transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul class="expandable-panel">
        <li class="expandable" disabled="true">
          <button class="expand-title">
            Disabled Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          </button>
          <div class="panel-content">
            <div class="content">
              Content
            </div>
          </div>
        </li>
        <li class="expandable" disabled="false">
          <button class="expand-title">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
          </button>
          <div class="panel-content">
            <div class="content">
              <form>
                <ul>
                  <li>
                    <label>Label</label>
                    <input type="text"/>
                  </li>
                  <li>
                    <label>Label</label>
                    <ul>
                      <li>
                        <input type="radio" id="default" name="radio" value="Default">
                        <label for="default">Item to Select</label>
                      </li>
                      <li>
                        <input type="radio" id="selected" name="radio" value="Selected" checked>
                        <label for="selected">Selected Item</label>
                      </li>
                      <li>
                        <input type="radio" id="disabled" name="radio" value="Disable">
                        <label for="disabled">Item to Select</label>
                      </li>
                    </ul>
                  </li>
                </ul>
              </form>
            </div>
          </div>
        </li>
      </ul>

问题是因为 .expandable 元素是 li,不能具有 disabled 属性。在它们上面使用 class,并在 :not 选择器中使用它:

$('.expandable-panel').find('.expandable').each(function() {
  if (!$(this).is('.disabled')) {
    $(this).find('.expand-title').click(function() {
      $(this).parent().toggleClass('expanded');
    });
  }
});
.expandable-panel {
  padding: 0;
  margin: 0;
  list-style: none;
}

li.disabled {
  background: red;
}

.expandable-panel > li {
  border: 1px solid #c7d0d8;
  border-radius: 3px;
  background: #ffffff;
  margin-bottom: 10px;
}
.expandable-panel > li.disabled {
  pointer-events: none;
}

.expandable-panel > li .expand-title {
  background: none;
  font-weight: normal;
  width: 100%;
  text-align: left;
  position: relative;
  padding-right: 36px;
}

.expandable-panel > li .expand-title:after {
  position: absolute;
  right: 16px;
  top: 14px;
  border: 5px transparent solid;
  content: ' ';
  border-top-color: #669BC6;
}

.expandable-panel > li:hover {
  border-color: #99A6B5;
}

.expandable-panel > li:hover .expand-title:after {
  border-top-color: #136FD5;
}

.expandable-panel > li .panel-content {
  visibility: hidden;
  opacity: 0;
  height: 0;
  transition: all 0.3s ease;
}

.expandable-panel > li .panel-content > .content {
  padding: 1.2em;
}

.expandable-panel .expanded {
  border: 1px solid #99A6B5;
}

.expandable-panel .expanded .panel-content {
  visibility: visible;
  opacity: 1;
  height: auto;
}

.expandable-panel .expanded .expand-title {
  border-bottom: 1px solid #99A6B5;
  border-radius: 3px 3px 0 0;
}

.expandable-panel .expanded .expand-title:after {
  top: 8px;
  border: 5px transparent solid;
  content: ' ';
  border-bottom-color: #669BC6;
}

.expandable-panel .expanded:hover .expand-title:after {
  border-top-color: transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul class="expandable-panel">
  <li class="expandable disabled">
    <button class="expand-title">
      Disabled Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </button>
    <div class="panel-content">
      <div class="content">
        Content
      </div>
    </div>
  </li>
  <li class="expandable">
    <button class="expand-title">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </button>
    <div class="panel-content">
      <div class="content">
        <form>
          <ul>
            <li>
              <label>Label</label>
              <input type="text" />
            </li>
            <li>
              <label>Label</label>
              <ul>
                <li>
                  <input type="radio" id="default" name="radio" value="Default">
                  <label for="default">Item to Select</label>
                </li>
                <li>
                  <input type="radio" id="selected" name="radio" value="Selected" checked>
                  <label for="selected">Selected Item</label>
                </li>
                <li>
                  <input type="radio" id="disabled" name="radio" value="Disable">
                  <label for="disabled">Item to Select</label>
                </li>
              </ul>
            </li>
          </ul>
        </form>
      </div>
    </div>
  </li>
</ul>

另请注意,我在禁用 .expandable 上设置了 pointer-events: none,这样点击甚至不会在 UI 中注册。