【问题标题】:trouble reading result of google customer search results object in javascript在 javascript 中无法读取 google 客户搜索结果对象的结果
【发布时间】:2020-05-26 17:58:07
【问题描述】:

有人知道在读取 Google 自定义搜索结果对象时使用的语法吗?

这是来自https://developers.google.com/custom-search/docs/element 的结果对象的 JSDoc 表示法:

{
  content: string,
  contentNoFormatting: string,
  contextUrl: string, // For image search results only
  fileFormat: string,
  image: { // For image search reseults only
    height: number,
    url: string,
    width: number,
  },
  perResultLabels: !Array<{
    anchor: string,
    label: string,
    labelWithOp: string,
  }>,
  richSnippet: !Array<!Object>, // For web search results only
  thumbnailImage: {
    height: number,
    url: string,
    width: number,
  },
  title: string,
  titleNoFormatting: string,
  url: string,
  visibleUrl: string,
} 

我需要访问“url”(thumbnailimage.url),但不确定引用它的语法。

这里是我需要使用它的地方。见下方评论

here is where I need to use it.  See comment below

const makeResultParts = (result) => {
const anchor = document.createElement('a');
anchor.href = result['thumbnailImage'] //  <<---this needs to be the thumbnailImage.url but that syntax does not work
anchor.target = '_blank';
anchor.classList.add('gs_title');
anchor.appendChild(document.createTextNode(result['visibleUrl']));
const span = document.createElement('span');
span.innerHTML = ' ' + result['title'];
return [anchor, span];
};

【问题讨论】:

    标签: javascript jsdoc google-custom-search


    【解决方案1】:

    假设result['thumbnailImage'] 始终存在(根据它应该用于图像搜索的文档),这应该适合您:

    const makeResultParts = (result) => {
      const anchor = document.createElement('a');
      anchor.href = result['thumbnailImage']['url'] //  <<--- Access the url attribute of the thumbnailImage entry
      anchor.target = '_blank';
      anchor.classList.add('gs_title');
      anchor.appendChild(document.createTextNode(result['visibleUrl']));
      const span = document.createElement('span');
      span.innerHTML = ' ' + result['title'];
      return [anchor, span];
    };

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 2018-02-01
      相关资源
      最近更新 更多