如何在html中添加工具提示?

时间:2013-06-29 11:46:10

标签: html css

我有以下代码,但它不起作用,任何人都可以帮助我

<form id="myform" action="#">

  <h3>Registration Form</h3>

  <div id="inputs">

    <!-- username -->
    <label for="username">Username</label>
    <input id="username" title="Must be at least 8 characters."/>
    <br />

    <!-- password -->
    <label for="password">Password</label>
    <input id="password" type="password" title="Make it hard to guess." />
    <br />

    <!-- email -->
    <label for="email">Email</label>
    <input id="email" title="We won't send you any marketing material." />
    <br />

    <!-- message -->
    <label for="body">Message</label>
    <textarea id="body" title="What's on your mind?"></textarea>
    <br />

    <!-- message -->
    <label for="where">Select one</label>
    <select id="where" title="Select one of these options">
      <option>-- first option --</option>
      <option>-- second option --</option>
      <option>-- third option --</option>
    </select>
    <br />
  </div>

  <!-- email -->
  <label>
    I accept the terms and conditions
    <input type="checkbox" id="check" title="Required to proceed" />
  </label>

  <p>
    <button type="button" title="This button won't do anything">
      Proceed
    </button>
  </p>

</form>

4 个答案:

答案 0 :(得分:4)

title属性可以正常工作。

Your code适用于chrome,FF和IE 9及10

答案 1 :(得分:2)

为什么不使用placeholder代替title

Supported overview

在其他浏览器中,您可以使用此javascript:

<script>
  var doc = document;
  var inputs = doc.getElementsByTagName('input'),
  var supportPlaceholder = 'placeholder' in doc.createElement('input');

  var placeholder = function(input) {
    var text = input.getAttribute('placeholder');
    var defaultValue = input.defaultValue;

    input.value = text;
    input.onfocus = function() {
      if (input.value === defaultValue || input.value === text) {
        this.value = '';
      }
    }
    input.onblur = function() {
      if (input.value === '') {
        this.value = text;
      }
    }
  };

  if (!supportPlaceholder) {
    for (var i = 0, len = inputs.length; i < len; i++) {
      var input = inputs[i], text = input.getAttribute('placeholder');
      if (input.type === 'text' && text) {
        placeholder(input);
      }
    }
  }
</script>

答案 2 :(得分:0)

插入title=""属性,它将适合您。

答案 3 :(得分:0)