javascript - 使用 JQuery 将 HTML 文本输入设置为 null

标签 javascript jquery html

使用 JQuery 事件处理程序,我想调整 HTML 文本输入的默认值并将其设置为空。

输入

<button id="setColor">Set Color</button>
<input type="text" id="colorText">

功能

$("#setColor").on("click", function ()
{
    document.getElementById('#colorText').defaultValue = "";
});

我也尝试过

$("#colorText").defaultValue = "";
$("#colorText").val = "";
$("#colorText").value = "";

但每次我单击“SetColor”按钮时,它似乎都会保留之前设置的默认值。

document.getElementById('colorText').defaultValue = "#0000ff";

最佳答案

我不清楚你的问题,但我在你的代码中遇到了一个简单的错误,你在 getElementById 中写了额外的#

试试这个:

$("#setColor").on("click", function ()
{
    document.getElementById('colorText').value = "";
});

您是否正在尝试重置文本框颜色或想要重置值?

关于javascript - 使用 JQuery 将 HTML 文本输入设置为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33327207/

相关文章:

javascript - 如何在 Pycharm/Intellij 中打印到 javascript 控制台

javascript - 通过 JSON 接收数据后的可编辑文本框

c# - asp.net mvc3 302 在 $.get 调用上发现错误

javascript - 如果 child 有类(class)事件,则为家长添加类(class)

javascript - 使用 jade 时出现错误 : "Failed to lookup view"

javascript - React 中的 Map 函数(err : TypeError: e. map is not a function)

javascript - 带有内联日期选择器(日历)的 JSON 数据类型

python - 用 Mechanize 打开网页后,如何继续提交

html - 将删除线元素保存到待办事项列表的本地存储

javascript - 如何使用 jquery 多选显示组名称以及下拉列表项?