使用 javascript 更改 HTML 标签属性
我试图在我的网站中获得我认为非常好的 HTML5 功能 - input
字段的占位符属性。
但我在网站上获取它的方式却严重退化。因为我跳过了字段的标签并使用占位符作为其标签。
目前我有几个带有占位符的输入字段,它们看起来像这样:
<input placeholder="hereismyplaceholder" />
但是如果不支持 HTML5 占位符,我希望所有带有占位符的输入字段都更改为此。显然,它们具有等效的占位符文本:
<input onblur="if (this.value == '') {this.value = 'hereismyplaceholder';}" onfocus="if (this.value == 'hereismyplaceholder') {this.value = '';}" value="hereismyplaceholder" />
所以我引入了 Modernizr,并且我使用以下 javascript 让它检测占位符:
if (Modernizr.input.placeholder) {
} else {
// BUT I NEED SOMETHING IN HERE
}
但我不知道现在该怎么办。因为我没有 JavaScript 经验,所以我不是开发人员。
最简单的解决方案将是最好的。我并不担心好的做法,我只是希望它现在能发挥作用。
I'm trying to get what I think is a really nice bit of HTML5 in my website - the placeholder attribute for input
fields.
But the way I've got it in my website degrades badly. Because I've skipped labels for the fields and used the placeholder as its label.
At the moment I have several input fields with placeholders, they look like this:
<input placeholder="hereismyplaceholder" />
But where HTML5 placeholder support is not available, I would like all of the input fields with placeholders to change to this. Obviously with their equivalent placeholder text:
<input onblur="if (this.value == '') {this.value = 'hereismyplaceholder';}" onfocus="if (this.value == 'hereismyplaceholder') {this.value = '';}" value="hereismyplaceholder" />
So I've pulled in Modernizr, and I'm getting it to detect the placeholders with the following javascript:
if (Modernizr.input.placeholder) {
} else {
// BUT I NEED SOMETHING IN HERE
}
But I have no idea what to do now. Because I have no experience with javascript, I'm not a developer.
The simplest solution would be the best please. I'm not fussed about good practice, I just want it to work for now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你没有要求 jQuery,但我不想担心跨浏览器问题:
You didn't ask for jQuery, but I didn't want to worry about cross-browser issues:
这里有一个例子, http://www.modernizr.com/docs/#input
对于您的特定用例,代码应该是
There is an example here, http://www.modernizr.com/docs/#input
For your specific use case, the code should be