我已将 Google 搜索框添加到我的 HTML5 网站。当我转到 W3C 验证器时,出现错误:元素 gcse:search 不允许作为元素 div 的子元素在此上下文中。谷歌指示我将脚本放在一个 div 中,没有它就无法让搜索框工作。任何人都可以帮忙吗?我想这是一个必须一直出现的问题。我的代码如下。非常感谢!
<div id="searchbox">
<script>
(function() {
var cx = '014531685129972083622:vk5kxnojpx0';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:search> </gcse:search>
</div>
最佳答案
I imagine it is a problem which must arise all the time
也许这就是他们在官方文档中涵盖这一点的原因……?
https://developers.google.com/custom-search/docs/element#html5 :
HTML5-valid
div
tagsYou can use HTML5-valid
div
tags as long as you observe these guidelines:
- The
class
attribute must be set togcse-XXX
- Any attributes must be prefixed with
data-
.For example:
<div class="gcse-searchbox" data-resultsUrl="http://www.example.com" data-newWindow="true" data-queryParameterName="search" >
关于javascript - Google 搜索框未验证为 HTML5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31083255/