当前位置:首页 > JavaScript

js实现一个按钮组件

2026-03-02 01:14:36JavaScript

实现一个基础按钮组件

使用原生JavaScript创建一个可复用的按钮组件,包含基础样式和点击事件。

class Button {
  constructor(text, onClick) {
    this.text = text;
    this.onClick = onClick;
    this.element = this.createButton();
  }

  createButton() {
    const button = document.createElement('button');
    button.textContent = this.text;
    button.style.padding = '8px 16px';
    button.style.borderRadius = '4px';
    button.style.backgroundColor = '#007bff';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.cursor = 'pointer';
    button.addEventListener('click', this.onClick);
    return button;
  }

  render(container) {
    if (container) {
      container.appendChild(this.element);
    }
    return this.element;
  }
}

添加按钮状态管理

扩展按钮组件,增加禁用状态和样式变化功能。

js实现一个按钮组件

class Button {
  constructor(text, onClick) {
    this.text = text;
    this.onClick = onClick;
    this.disabled = false;
    this.element = this.createButton();
  }

  createButton() {
    const button = document.createElement('button');
    button.textContent = this.text;
    this.applyBaseStyles(button);
    button.addEventListener('click', () => {
      if (!this.disabled) {
        this.onClick();
      }
    });
    return button;
  }

  applyBaseStyles(button) {
    button.style.padding = '8px 16px';
    button.style.borderRadius = '4px';
    button.style.border = 'none';
    button.style.cursor = 'pointer';
    button.style.transition = 'background-color 0.3s';
    this.updateButtonStyle();
  }

  updateButtonStyle() {
    if (this.disabled) {
      this.element.style.backgroundColor = '#cccccc';
      this.element.style.cursor = 'not-allowed';
    } else {
      this.element.style.backgroundColor = '#007bff';
      this.element.style.cursor = 'pointer';
    }
  }

  setDisabled(disabled) {
    this.disabled = disabled;
    this.updateButtonStyle();
  }

  render(container) {
    if (container) {
      container.appendChild(this.element);
    }
    return this.element;
  }
}

使用CSS类代替内联样式

改进组件,使用CSS类管理样式,提高可维护性。

class Button {
  constructor(text, onClick) {
    this.text = text;
    this.onClick = onClick;
    this.disabled = false;
    this.element = this.createButton();
  }

  createButton() {
    const button = document.createElement('button');
    button.textContent = this.text;
    button.classList.add('btn');
    button.addEventListener('click', () => {
      if (!this.disabled) {
        this.onClick();
      }
    });
    this.updateButtonStyle();
    return button;
  }

  updateButtonStyle() {
    if (this.disabled) {
      this.element.classList.add('btn-disabled');
      this.element.classList.remove('btn-active');
    } else {
      this.element.classList.add('btn-active');
      this.element.classList.remove('btn-disabled');
    }
  }

  setDisabled(disabled) {
    this.disabled = disabled;
    this.updateButtonStyle();
  }

  render(container) {
    if (container) {
      container.appendChild(this.element);
    }
    return this.element;
  }
}

配套CSS样式:

js实现一个按钮组件

.btn {
  padding: 8px 16px;
  border-radius: 4px;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn-active {
  background-color: #007bff;
  color: white;
}

.btn-disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

添加按钮类型支持

扩展组件支持不同按钮类型(主按钮、次按钮、危险按钮等)。

class Button {
  constructor(text, onClick, options = {}) {
    this.text = text;
    this.onClick = onClick;
    this.disabled = options.disabled || false;
    this.type = options.type || 'primary';
    this.element = this.createButton();
  }

  createButton() {
    const button = document.createElement('button');
    button.textContent = this.text;
    button.classList.add('btn', `btn-${this.type}`);
    button.addEventListener('click', () => {
      if (!this.disabled) {
        this.onClick();
      }
    });
    this.updateButtonStyle();
    return button;
  }

  updateButtonStyle() {
    this.element.classList.toggle('btn-disabled', this.disabled);
  }

  setDisabled(disabled) {
    this.disabled = disabled;
    this.updateButtonStyle();
  }

  setType(type) {
    this.element.classList.remove(`btn-${this.type}`);
    this.type = type;
    this.element.classList.add(`btn-${this.type}`);
  }

  render(container) {
    if (container) {
      container.appendChild(this.element);
    }
    return this.element;
  }
}

配套CSS样式扩展:

.btn-primary {
  background-color: #007bff;
  color: white;
}

.btn-secondary {
  background-color: #6c757d;
  color: white;
}

.btn-danger {
  background-color: #dc3545;
  color: white;
}

.btn-success {
  background-color: #28a745;
  color: white;
}

组件使用方法示例

// 创建按钮实例
const myButton = new Button('点击我', () => {
  console.log('按钮被点击');
}, {
  type: 'primary'
});

// 渲染到页面
myButton.render(document.body);

// 禁用按钮
myButton.setDisabled(true);

// 改变按钮类型
myButton.setType('danger');

这些实现展示了如何从简单到复杂逐步构建一个可复用的JavaScript按钮组件,包含样式管理、状态控制和类型支持等功能。

标签: 组件按钮
分享给朋友:

相关文章

vue实现组件拖放

vue实现组件拖放

Vue 实现组件拖放的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现倒计时组件

vue实现倒计时组件

Vue 倒计时组件实现 核心思路 通过 setInterval 或 setTimeout 实现时间递减,结合 Vue 的响应式数据更新 UI。需注意组件销毁时清除定时器。 基础实现方案 模板部分…

uniapp组件写法

uniapp组件写法

Uniapp 组件的基本写法 Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template>、<script>…

vue实现按钮实现点击功能

vue实现按钮实现点击功能

实现按钮点击功能 在Vue中实现按钮点击功能可以通过v-on指令或@缩写来绑定事件。以下是几种常见的实现方式: 使用v-on指令 通过v-on:click绑定点击事件,触发定义在methods中的…

vue表格组件实现

vue表格组件实现

Vue表格组件实现 基础表格实现 使用el-table组件实现基础表格功能。安装Element UI后,引入el-table和el-table-column组件。 <template>…

vue实现递归组件

vue实现递归组件

递归组件的实现方法 在Vue中实现递归组件主要依靠组件调用自身的能力。以下是几种常见的实现方式: 使用组件name属性 通过组件的name属性实现递归调用是最简单的方式: <templa…