【问题标题】:How can I get element by title in iframes without jQuery? [duplicate]如何在没有 jQuery 的 iframe 中按标题获取元素? [复制]
【发布时间】:2019-10-02 17:29:49
【问题描述】:

我在使用 jQuery 时遇到了一些性能问题。因此,我想尝试使用 JS 而不是 jQuery 的 7000 长度的循环。因为我读过,那个 jQuery 的性能总是很差。

我尝试将我的 jQuery 选择器转换为 JS,但它仍然无法正常工作:

来自:

var i;
for (i = 0; i < e.detail.length; i++){
    $("iframe").contents().find(".timeline-node[title='" + i + "']").css("background-image", "url( \"imgs/" + e.detail[i] + ".png \") ");
}

到:

var i;
for (i = 0; i < e.detail.length; i++){
   document.getElementById('#iframe').querySelector("[title=\"" + i + "\"]").css("background-image", "url( \"imgs/quality_3/" + e.detail[i] + ".png \") ");
}

我的新代码的错误是:Cannot read property 'querySelector' of null at HTMLDocument。 我认为 JS 无法使用 attr 找到我的标题。一世。但我的 jQuery 代码运行良好,但需要 80 秒。为 7000 个索引执行此操作。

【问题讨论】:

  • document.getElementById('#iframe') 更改为 document.querySelector('iframe')
  • document.getElementById('iframe') OR document.querySelector('#iframe') 如果 iframe 具有 id="iframe" 否则 document.querySelector('iframe')
  • @mplungjan 从初始循环中,您可以看到他选择的是元素,而不是 ID:$("iframe")
  • 是的,但我的评论仍然正确:)
  • 另外,对于将来的 jquery 到 vanilla js 的转换,检查这个网站总是有帮助的:youmightnotneedjquery.com

标签: javascript jquery


【解决方案1】:

只查询一次iframe

var i, iframe = document.querySelector('iframe').contentWindow.document;
for (i = 0; i < e.detail.length; i++){
   iframe.querySelector("[title=\"" + i + "\"]").style.backgroundImage = "url( \"imgs/quality_3/" + e.detail[i] + ".png \") ";
}

您可以尝试只使用一个查询并过滤结果

document.querySelector("iframe").contentWindow.document.querySelectorAll("[title]").forEach(function (elm) {
    var id = +elm.getAttribute("title");
    if (id < e.detail.length) {
        elm.style.backgroundImage = "url( \"imgs/quality_3/" + e.detail[id] + ".png \") ";
    }
});

多 iframe 版本

document.querySelectorAll("iframe").forEach(function (iframe) {
    iframe.contentWindow.document.querySelectorAll("[title]").forEach(function (elm) {
        var id = +elm.getAttribute("title");
        if (id < e.detail.length) {
            elm.style.backgroundImage = "url( \"imgs/quality_3/" + e.detail[id] + ".png \") ";
        }
    })
});

性能对比

console.time("loop");
var i;
for(var a = 0; a < 10; a++) {
  for (i = 0; i < 20; i++){
     document.querySelector("[title=\"" + i + "\"]");
  }
}
console.timeEnd("loop")

console.time("singleQuery");
var i;
for(var a = 0; a < 10; a++) {
   document.querySelectorAll("[title]").forEach(function(elm) { 
     if(+elm.getAttribute("title") < 20) { 
      // change background 
     }
   });
}
console.timeEnd("singleQuery")
<p title="0"></p>
<p title="1"></p>
<p title="2"></p>
<p title="3"></p>
<p title="4"></p>
<p title="5"></p>
<p title="6"></p>
<p title="7"></p>
<p title="8"></p>
<p title="9"></p>
<p title="10"></p>
<p title="11"></p>
<p title="12"></p>
<p title="13"></p>
<p title="14"></p>
<p title="15"></p>
<p title="16"></p>
<p title="17"></p>
<p title="18"></p>
<p title="19"></p>

【讨论】:

  • @mplungjan Dupe 是关于获取 iframe 内容,而不是性能提升
  • 第一个问题是得到它,不是吗?然后将 7000 张图片插入 DOM 将需要一段时间
  • @mplungjan OP 说脚本在 80 秒内执行,在我的第二个示例中它应该更快
  • 哇,现在看起来很不错,但我只有一个错误:“无法在 HTMLDocument 中读取 null 的属性 'style'”。但是我所有带有元素标题的 Div 都有一个 style=""。你知道为什么 JS 找不到样式吗?
  • @mplungjan 是的,我测试过。校验码sn-p
猜你喜欢
  • 2011-03-06
  • 2018-01-13
  • 2021-11-10
  • 2017-07-14
  • 2021-09-04
  • 2017-02-16
  • 1970-01-01
  • 2023-03-10
  • 2011-12-11
相关资源
最近更新 更多