2010-10-12 87 views

回答

15

如果您使用的是Firebug,我可以假定您使用Firefox,并且Firefox在输入字段本身中尚不支持placeholder属性。

Placeholder feature detection

我只是想在Chrome的Mac,它支持对文本域占位符文本(和通过JavaScript改变)

2015年更新:有时我得到这个答案的声誉,所以我想澄清,这被接受为正确的,因为当时Firefox 3.6不支持占位符属性。然后第4版增加了支持(“解决问题”不太公平),因此OP的代码按照预期工作。

49

试试这个:

$('#inputTextID').attr("placeholder","placeholder text"); 
18

我认为你必须这样做:

$("textarea").val(''); 
$("textarea").attr("placeholder", "New placeholder text"); 
+2

清除值有助于。 – neelmeg 2013-10-08 15:59:37

+0

对于未来的访客来说,清晰的价值首先是它做了什么。 Chrome 40.0.2214.93 – BenRacicot 2015-01-29 19:49:25

+0

有没有办法从var设置占位符文本? ex: 'var $ text ='abc'; $('#id')。attr(“placeholder”,$ text);' – shaneparsons 2015-03-03 22:02:00

2
<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文本This is new text

2

工作示例使用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"); 
} 
} 
-1

HTML动态占位符:

<input type="text" placeholder="entername" id="fname"/> 

JS:

$("#fname").attr("placeholder", "New placeholder text"); 
相关问题