javascript - value清空后如何添加回占位符?

标签 javascript jquery html

清除文本区域的值后,占位符丢失。我需要做的就是将该占位符添加回文本区域。

html:

<textarea data-reply placeholder="Add your reply" autocomplete="off" name="message" ></textarea>

clear the textarea value

$('[data-reply]').val(' ');

adding placeholder (doesn't work)

1) $('[data-reply]').attr("placeholder", "New placeholder text");


2) $("textarea[placeholder]").attr("placeholder", "New placeholder text");

此处引用:Changing the "placeholder" attribute of HTML5 input elements dynamically using Javascript

最佳答案

您无需执行任何操作。清除该值后,浏览器将显示占位符。

您放入文本区域的值不会清除它。它是一个空格字符。

$('[data-reply]').val(' ');

尝试:

$('[data-reply]').val('');

关于javascript - value清空后如何添加回占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40280806/

相关文章:

javascript - 是否可以给 var 和innerHTML id?

html - 组合两种 CSS 效果

javascript - 如何在形成正则表达式来比较两个句子时仅忽略动态内容的空格?

javascript - d3 对立图表 Axis 对齐问题

jquery - CSS 过渡 Chrome 问题

javascript - jquery刷新页面,然后做一些事情

jquery - 使用 jquery 将高度添加到带有 !important 在 css 中的 div

javascript - React 如何从输入字段中获取表单数据

javascript - 使用turn.js显示双页

javascript - 动态调用 jQuery 函数