我正在尝试placeholder
使用 jQuery 动态更新文本字段的 HTML5 属性。
$("textarea").attr("placeholder", "New placeholder text");
从 Firebug 中,我可以观察到该placeholder
属性确实在发生变化。但在渲染textarea
元素中,它保持不变。有什么建议么?
我正在尝试placeholder
使用 jQuery 动态更新文本字段的 HTML5 属性。
$("textarea").attr("placeholder", "New placeholder text");
从 Firebug 中,我可以观察到该placeholder
属性确实在发生变化。但在渲染textarea
元素中,它保持不变。有什么建议么?
试试这个 :
$('#inputTextID').attr("placeholder","placeholder text");
我认为你必须这样做:
$("textarea").val('');
$("textarea").attr("placeholder", "New placeholder text");
如果您使用的是 Firebug,我可以假设您使用的是 Firefox,而 Firefox 尚不支持placeholder
输入字段本身的属性。
我刚刚在 Chrome for Mac 上尝试过,它支持 textareas 上的占位符文本(并通过 javascript 进行更改)
2015 更新:有时我会因为这个答案而获得声誉,所以我想澄清一下,这被认为是正确的,因为当时 Firefox 3.6 不支持占位符属性。Release 4 然后添加了支持(“修复了问题”并不公平),因此 OP 的代码从那时起按预期工作。
<label for="myname">Name *</label>
<input type="text" name="myname" id="myname" title="Please enter your name"
pattern="[A-Za-z ]+, [A-Za-z ]+"
autofocus required placeholder="Last, First" />
然后您要选择占位符并将该文本替换为new text that you want to enter
.
$("input[placeholder]").attr("placeholder", "This is new text");
这将替换Last, First
to的文本This is new text
。
使用 Javascript 和 Jquery http://jsfiddle.net/ogk2L14n/1/的动态占位符的工作示例
<input type="text" id="textbox">
<select id="selection" onchange="changeplh()">
<option>one</option>
<option>two</option>
<option>three</option>
</select>
function changeplh(){
debugger;
var sel = document.getElementById("selection");
var textbx = document.getElementById("textbox");
var indexe = sel.selectedIndex;
if(indexe == 0) {
$("#textbox").attr("placeholder", "age");
}
if(indexe == 1) {
$("#textbox").attr("placeholder", "name");
}
}
HTML:
<input type="text" placeholder="entername" id="fname"/>
JS:
$("#fname").attr("placeholder", "New placeholder text");