You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将input输入框内文本设置为删除线样式?

给HTML输入框添加删除线样式

要让<input type="text">里的文本显示成删除线效果,直接用CSS的text-decoration: line-through属性就能实现,下面是几种常用方法:

方法1:内联样式直接设置

直接在input标签里加style属性,快速生效:

<input type="text" value="Hello World!" style="text-decoration: line-through;" />

方法2:通过CSS类统一控制

如果多个输入框需要相同样式,定义CSS类更便于维护:

<style>
  .strike-input {
    text-decoration: line-through;
  }
</style>
<input type="text" value="Hello World!" class="strike-input" />

方法3:JavaScript动态控制

如果需要根据交互(比如点击按钮)动态添加/移除删除线,可以用JS操作:

// 获取目标输入框
const targetInput = document.querySelector('input[type="text"]');

// 添加删除线
targetInput.style.textDecoration = 'line-through';
// 或者通过添加类名的方式
// targetInput.classList.add('strike-input');

// 移除删除线的话可以用:
// targetInput.style.textDecoration = 'none';
// targetInput.classList.remove('strike-input');

注意事项

主流浏览器(Chrome、Firefox、Edge等)都完美支持text-decoration在input元素上的应用,无需额外兼容处理。如果遇到极个别老旧浏览器渲染异常,可以尝试用伪元素模拟,但这种场景非常少见。

内容的提问来源于stack exchange,提问作者sina pouladrag

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 09:27:24