如何在JS迭代中定义querySelectorAll

时间:2017-05-02 08:42:01

标签: javascript polymer iteration

Inside Polymer,我需要查找具有某些类名的所有<a>并将其ID添加为innerHTML,但我似乎无法弄明白。类名在外部json文件中,通过iron-ajax加载。

现在我有这个功能:

Polymer({
  is: 'settings-menu',
  properties: {
            paragraphTitles: {
              type: Object,
              notify: true,
              value: function(){return []}
            },
            selectedParagraph: {
                type: String,
                computed: "_computePar(paragraphTitles)"
            }
  },
  _computePar: function(paragraphTitles) {
    for (var key in paragraphTitles) {
            var refs = document.querySelectorAll('a.',key);
            refs.forEach(item => item.innerHTML = item.id);
    }
  },

加载到paragraphTitles中的json文件如下所示:

{
        "as": "Page numbers in Author1, 1972.",
        "adh-v": "Paragraph numbers in Some Other Author 1978",
        "apz": "Paragraph numbers in Author 3",
        "cps": "Paragraph numbers in Author 4, 1952-62",
}

querySelectorAll的搜索字符串显然是错误的,但如何输入正确的搜索字符串?

1 个答案:

答案 0 :(得分:1)

Actually not sure what you want to archive please post some more information. But what definitly is wrong with your code is that

document.querySelectorAll()

is better not used since this will break Shadow Dom. Instead use

Polymer.dom(this).querySelectorAll()