最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google's html5 shiv not rendering placeholders? - Stack Overflow

programmeradmin2浏览0评论

I have a form which the client wants the labels inside the input box and I decided to use HTML 5's placeholder and Google's html5shiv, instead of the old javascript. My placeholders, however, don't seem to work properly in IE, which is what I was using the shiv for. Here is the code:

Doctype:

<!DOCTYPE html>

Shiv:

<!--[if IE]>
    <script src=".js" type="text/javascript"></script>
<![endif]-->

Form:

<form method="post" enctype="multipart/form-data" name="contactForm" id="contactForm">

    <input type="text" name="name" id="name" placeholder="Your Full Name..." />
    <input type="text" name="telephone" id="telephone" placeholder="Your Telephone Number..." />
    <input type="email" name="email" id="email" placeholder="Your Email Address..." />
    <textarea name="enquiry" id="enquiry" placeholder="Your Enquiry or Comments..."></textarea>
    <p class="midpadLeft green_Button_margin_2"><span class="green_Button_2"><a href="javascript: document.contactForm.submit();" target="_self" class="green_Button_2">Submit Enquiry <img src="images/mid-envelope.png" alt="Submit"/></a></span></p>

</form>

Any ideas as to why it's not working?

Revision:

Using code suggested in the comments below, I have changed my code to the following, but it is still not working.

Script including (the contents of that script was copied directly from here):

<script type="text/javascript" src="scripts/placeholders.js" charset="utf-8"></script>

DOM ready event listener:

<body onload="Placeholders.init(true);">

I have a form which the client wants the labels inside the input box and I decided to use HTML 5's placeholder and Google's html5shiv, instead of the old javascript. My placeholders, however, don't seem to work properly in IE, which is what I was using the shiv for. Here is the code:

Doctype:

<!DOCTYPE html>

Shiv:

<!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js" type="text/javascript"></script>
<![endif]-->

Form:

<form method="post" enctype="multipart/form-data" name="contactForm" id="contactForm">

    <input type="text" name="name" id="name" placeholder="Your Full Name..." />
    <input type="text" name="telephone" id="telephone" placeholder="Your Telephone Number..." />
    <input type="email" name="email" id="email" placeholder="Your Email Address..." />
    <textarea name="enquiry" id="enquiry" placeholder="Your Enquiry or Comments..."></textarea>
    <p class="midpadLeft green_Button_margin_2"><span class="green_Button_2"><a href="javascript: document.contactForm.submit();" target="_self" class="green_Button_2">Submit Enquiry <img src="images/mid-envelope.png" alt="Submit"/></a></span></p>

</form>

Any ideas as to why it's not working?

Revision:

Using code suggested in the comments below, I have changed my code to the following, but it is still not working.

Script including (the contents of that script was copied directly from here):

<script type="text/javascript" src="scripts/placeholders.js" charset="utf-8"></script>

DOM ready event listener:

<body onload="Placeholders.init(true);">
Share Improve this question edited Feb 14, 2012 at 13:29 Phil Young asked Feb 14, 2012 at 12:48 Phil YoungPhil Young 1,3543 gold badges21 silver badges44 bronze badges 10
  • 1 There does not seem to be any reference to placeholder attributes in the code. Is html5shiv actually supposed to shim them? – pimvdb Commented Feb 14, 2012 at 12:53
  • 1 As far as I'm aware html5shiv doesn't do anything for the placeholder attribute. There are various shims available though (mostly jQuery)... see Modernizr for a list, or my own attempt (which doesn't require jQuery) – James Allardice Commented Feb 14, 2012 at 12:53
  • @JamesAllardice I have tried your code, but it's not working. Please see revision – Phil Young Commented Feb 14, 2012 at 13:25
  • @PhilFaceplantYoung - See what revision? – James Allardice Commented Feb 14, 2012 at 13:28
  • The one I just added, sorry, I should have done that in the opposite order – Phil Young Commented Feb 14, 2012 at 13:30
 |  Show 5 more comments

2 Answers 2

Reset to default 22

The HTML5 Shiv simply enables styling for previously unknown elements in IE. The most recent version does a few things extra, like patching document.createElement, but other than that it doesn’t polyfill anything.

If you want to emulate placeholder, use a placeholder polyfill. I’ve written one in jQuery plugin format, if you’re interested: http://mths.be/placeholder

Use it as follows:

$('input, textarea').placeholder();

Here’s a demo: http://mathiasbynens.be/demo/placeholder

Btw, you should be using <label> instead of @placeholder if the text is “Your Full Name” etc.

Check out Todd Northrop's jquery.watermark NuGet package too.

//polyfill placeholder in older browsers
var inputs = $('input, textarea');

$.each(inputs, function (i, itm) {
    var input = $(itm);
    input.watermark(input.attr('placeholder'));
});

I have compared both methods provided, and watermark seems to cope with Password inputs better, but its not quite as easy to set up watermark.

发布评论

评论列表(0)

  1. 暂无评论