当前位置:首页 > 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;
  }
}

添加按钮状态管理

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

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样式:

.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样式扩展:

js实现一个按钮组件

.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按钮组件,包含样式管理、状态控制和类型支持等功能。

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

相关文章

css按钮制作

css按钮制作

CSS按钮制作方法 基础按钮样式 创建一个简单的CSS按钮需要定义基本属性,如背景色、边框、内边距和文字样式。示例代码: .btn { background-color: #4CAF50;…

组件elementui

组件elementui

ElementUI 简介 ElementUI 是一套基于 Vue.js 的桌面端组件库,由饿了么前端团队开发并开源。它提供了丰富的 UI 组件(如按钮、表单、表格等),适合快速构建企业级中后台产品。…

如何缓存react组件

如何缓存react组件

缓存React组件的方法 React.memo 使用React.memo对函数组件进行浅比较缓存,避免不必要的重新渲染。适用于props不变的场景。 const MemoizedComponen…

vue实现分页组件

vue实现分页组件

Vue 分页组件实现 基础分页组件结构 创建一个名为 Pagination.vue 的文件,包含以下基础模板和逻辑: <template> <div class="pagina…

vue实现搜索组件

vue实现搜索组件

Vue 实现搜索组件的方法 基础搜索组件实现 创建基础搜索组件需要包含输入框和搜索按钮。在Vue中可以使用v-model绑定输入值,通过@click或@keyup.enter触发搜索。 <te…

vue 实现动态组件

vue 实现动态组件

Vue 动态组件的实现方法 在 Vue 中,可以通过 <component> 标签结合 is 属性实现动态组件切换。以下是具体实现方式: 使用 is 属性绑定组件名 <templ…