autocomplete attribute is not passing XHTML 1.0 Transitional validation, why?

和自甴很熟 提交于 2019-12-07 12:16:39

问题


I'm trying to cleanup my xhtml validation -- I'm running my pages through the W3C validator. For some puzzling reason it's not passing on input fields with the autocomplete="off" attribute:

<input name="kwsearch" id="sli_search_1" type="text" autocomplete="off" onfocus="if(this.defaultValue==this.value) this.value='';"
            onblur="if(this.value=='')this.value=this.defaultValue;" class="searchbox" value="Search" />

I'm using this doctype:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

And this is the validation error:

Line 410, Column 81: there is no attribute "autocomplete"

…li_search_1" type="text" autocomplete="off" onfocus="if(this.defaultValue==thi…

I thought this was okay with the W3C -- but, maybe it's still in "submission" phase? http://www.w3.org/Submission/web-forms2/#autocomplete

Thoughts?


回答1:


The Web forms specification has nothing to do with HTML 4 / XHTML. Sadly, autocomplete will not pass validation.

I think the only way to achieve valid HTML 4 /XHTML with autocomplete turned off is adding the attribute on page load using JavaScript. Sucks, I know - but I think it's the only way.




回答2:


autocomplete is a HTML5 attribute, so use a HTML5 document type declaration, if you need it.




回答3:


That W3C link is for the web forms stuff, not core XHTML. It might be possible to pull in the extra DTD for the web forms and get the page to validate.




回答4:


If you need autocomplete( browsers do support it ), then try extending your doctype, like in this XHTML 1.1 below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" [ <!ATTLIST form autocomplete (on|off) #IMPLIED> ]>




回答5:


I've just banged up against this irritating conflict between browsers and standards. I ended up getting around it by running javascript ON THE PAGE, not waiting for window.onLoad or $(document).ready(), to add the attribute to all elements with the class no-browser-autocomplete. Then i went through my app removing autocomplete="off" and adding this class instead.

Obviously this will fail in browser environments not running javascript.

The reason that i do it on the page, rather than in a dom ready block, is that if you wait for dom ready, the browser's already autocompleted it, at least in Firefox (which i'm testing it in).

So, this is at the start of one of the javascript files i include in my app layout:

//this needs to run BEFORE all of the loaded/ready events fire, that's why it's not in the dom.ready function
$(".no-browser-autocomplete").attr("autocomplete", "off");

$(function(){ 
  //dom ready
});


来源:https://stackoverflow.com/questions/2516364/autocomplete-attribute-is-not-passing-xhtml-1-0-transitional-validation-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!