如何使用纯Javascript单击另一个按钮时从按钮中删除类

时间:2017-12-03 14:59:05

标签: javascript css class toggle

我很难让它发挥作用。

我有一个响应标签/手风琴。但是,当点击一个按钮时,该课程会激活"应用了,但是当点击另一个按钮时,"活动"类仍然保留在上一个按钮上。我必须使用Javascript而不是jQuery来实现这一点。

我收到错误"未捕获的TypeError:无法读取属性'删除'未定义的     在HTMLButtonElement。"

此处是我的代码:

var accordion = document.getElementsByClassName("accordion");
var activeAccordion = document.getElementsByTagName("button");
var i;

for (i = 0; i < accordion.length; i++) {

  accordion[i].addEventListener("click", function(){

    this.classList.toggle("active");
    activeAccordion.classList.remove("active");

    var panel = this.nextElementSibling;
     if (panel.style.maxHeight){
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    } 
  }) 
}

1 个答案:

答案 0 :(得分:0)

替换此

import UIKit

@IBDesignable
class RecordView: UIView {

    @IBInspectable
    var borderColor: UIColor = UIColor.clear {
        didSet {
            self.layer.borderColor = borderColor.cgColor
        }
    }

    @IBInspectable
    var borderWidth: CGFloat = 20 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }

    @IBInspectable
    var cornerRadius: CGFloat = 100 {
        didSet {
            layer.cornerRadius = cornerRadius
        }
    }

    private var fillView = UIView()

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupFillView()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupFillView()
    }



    private func setupFillView() {
        let radius = (self.cornerRadius - self.borderWidth) * 0.95
        fillView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: radius * 2, height: radius * 2))
        fillView.center = CGPoint(x: self.bounds.midX, y: self.bounds.midY)
        fillView.layer.cornerRadius = radius
        fillView.backgroundColor = UIColor.red
        self.addSubview(fillView)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
    }

    func didClick() {
        UIView.animate(withDuration: 1.0, animations: {
            self.fillView.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
        }) { (true) in
            print()
        }
    }
}

通过

UIView