[英]How to set placeholder with JS
Please take a look at this fiddle: 请看一下这个小提琴:
http://jsfiddle.net/KA85s/3/ http://jsfiddle.net/KA85s/3/
JQ-UI sets first option as default. JQ-UI将第一个选项设置为默认选项。 I don't want it to be happened: instead I want to set HTML5 placeholder.
我不希望发生这种情况:相反,我想设置HTML5占位符。
How can I set HTML 5 placeholder (instead of first available <option>
) for these JS generated inputs? 如何为这些JS生成的输入设置HTML 5占位符(而不是第一个可用的
<option>
)?
Use: 使用:
input.attr("placeholder", value);
Updated jsfiddle: 更新了jsfiddle:
http://jsfiddle.net/Meligy/KA85s/5/ http://jsfiddle.net/Meligy/KA85s/5/
In the example I just replaced your val()
call, obviously, you can completely remove the part from the function and add it separately with any text you want. 在本例中,我只是替换了
val()
调用,显然,您可以从函数中完全删除该部分,并与所需的任何文本分别添加。
This will do all you need without cross browser compatibility issue i have tested in ie7,8,9 runs perfect. 这将满足您的所有需要,而不会出现我在ie7、8、9中测试过的跨浏览器兼容性问题。
<input type="text" value="Search" onfocus="if (this.value == 'Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search';}">
good luck! 祝好运!
@Mohamed Meligy came with the same solution @Mohamed Meligy提供了相同的解决方案
http://jsfiddle.net/KA85s/8/ http://jsfiddle.net/KA85s/8/
Though i used something else.. 虽然我用了别的东西..
here is how it works when your input element is created with use jQuery to give it a placholder attribute and we are done :D 当使用jQuery创建输入元素并为其赋予placholder属性时,这就是它的工作原理:D
** UPDATED ** ** 更新 **
http://jsfiddle.net/KA85s/19/ http://jsfiddle.net/KA85s/19/
added custom default value :D 添加了自定义默认值:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.