简体   繁体   English

Google自定义搜索引擎结果页上的缩略图alt属性

[英]Thumbnail Image alt Attributes on Google Custom Search Engine Results Page

Running through some accessibility testing and seeing that GCSE results are displaying thumbnail images with no alt attribute. 通过一些辅助功能测试,发现GCSE结果显示的缩略图图像没有alt属性。 It looks like I'll have to use javascript to add this attribute, but I feel dirty in the way I've gone about doing this. 看来我必须使用javascript来添加此属性,但是在执行此操作时,我感到很脏。

I know about firing a callback function via: 我知道通过以下方式触发回调函数:

window.__gcse = {
  callback: myCallback
};

And I am able to fire a function, but I seem to have to use a setTimeout() function to ensure the images are there. 而且我能够触发一个函数,但是我似乎不得不使用setTimeout()函数来确保图像在那里。 I've tried using both a jQuery load() as well as on.('load', fn...) but neither seem to do the job of adding the attribute. 我试过同时使用jQuery load()和on。('load',fn ...),但似乎都没有完成添加属性的工作。 However, using a setTimeout to give the search results some time to generate does work, but I'm afraid of this being a dirty hack. 但是,使用setTimeout给搜索结果一些时间来生成确实可以,但是我担心这是一个肮脏的hack。

// add some alt tags after google custom search has loaded results
function myCallback() {
  setTimeout(function() {jQuery('.gs-image').attr('alt','Article Thumbnail');}, 500); // empty string better than generic text
  setTimeout(function() {jQuery("img.gsc-branding-img").attr('alt','Google');}, 500);
  setTimeout(function() {jQuery(".gsc-search-button-v2").attr('alt','Search');}, 500);
}

Any ideas? 有任何想法吗?

I have been able to add alt text for accessibility for the magnifying glass search button, but so far I have not been able to add it for the Google branding image. 我已经能够为放大镜搜索按钮添加可替代性文本,但是到目前为止,我还不能为Google品牌形象添加它。 I got this idea from https://productforums.google.com/forum/?hl=en#!topic/customsearch/Owey1Sj6oB8;context-place=forum/customsearch 我从https://productforums.google.com/forum/?hl=zh_CN!topic/customsearch/Owey1Sj6oB8;context-place=forum/customsearch获得这个想法

(Be sure to replace 000000000000000000000:aaaaaaaaa with your own Google Custom Search Engine ID.) (请确保将000000000000000000000:aaaaaaaaa替换为您自己的Google自定义搜索引擎ID。)

<script>var myCallback = function() {   
document.getElementsByClassName('gsc-search-button gsc-search-button-v2')[0].alt = "Search Icon"; }; 
window.__gcse = {   callback: myCallback };   
  (function() {
    var cx = '000000000000000000000:aaaaaaaaa';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//cse.google.com/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:search linktarget="_parent"></gcse:search>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM