使用 javascript 更改 HTML 标签属性

发布于 2024-11-07 18:02:18 字数 827 浏览 0 评论 0原文

我试图在我的网站中获得我认为非常好的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-11-14 18:02:18

你没有要求 jQuery,但我不想担心跨浏览器问题:

if (!Modernizr.input.placeholder) {
  $("input[type=text]")
  .focus(function(e){
      if (this.value == $(this).attr('placeholder')) {
         this.value = '';
      }         
  })
  .blur(function(e){
    setPlaceholder(this);
  }).each(function(index, el){
    setPlaceholder(el);
  });

  function setPlaceholder(el) {
    if (el.value  == '') {
        el.value = $(el).attr('placeholder');
    }
  }
}

You didn't ask for jQuery, but I didn't want to worry about cross-browser issues:

if (!Modernizr.input.placeholder) {
  $("input[type=text]")
  .focus(function(e){
      if (this.value == $(this).attr('placeholder')) {
         this.value = '';
      }         
  })
  .blur(function(e){
    setPlaceholder(this);
  }).each(function(index, el){
    setPlaceholder(el);
  });

  function setPlaceholder(el) {
    if (el.value  == '') {
        el.value = $(el).attr('placeholder');
    }
  }
}
捎一片雪花 2024-11-14 18:02:18

这里有一个例子, http://www.modernizr.com/docs/#input

对于您的特定用例,代码应该是

// if placeholder isn't supported:
if (!Modernizr.input.placeholder){
  // use a input hint script
  setInputHint(document.getElementById('idoftheinputelement'),'hereismyplaceholder');
}

There is an example here, http://www.modernizr.com/docs/#input

For your specific use case, the code should be

// if placeholder isn't supported:
if (!Modernizr.input.placeholder){
  // use a input hint script
  setInputHint(document.getElementById('idoftheinputelement'),'hereismyplaceholder');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文