我无法让 className 属性在 Safari 上工作

发布于 2025-01-07 06:09:14 字数 694 浏览 0 评论 0 原文

在添加 a.className = "year"; 之前,我尝试了这个 javascript 函数,一切正常,但是添加它后,我无法使用 Safari 或 Google Chrome 加载页面。我需要做的只是向新的 a 元素添加一个类,我也尝试使用 a.setAttribute("class", "year"); 但我得到了同样的错误:页面无法加载。如果有人知道如何帮助我,我将非常感激!

function create_timeline() {

    timeline = document.getElementById("timeline_ul");
    years = document.getElementsByClassName("year");
    for (i=0;i<years.length;i++) {
        if (years.item(i).nodeType==1) {
            year = years.item(i).innerHTML;
            a = document.createElement("a");
            a.innerHTML = year;
            a.className = "year";
            timeline.appendChild(a);
        }
    }

}

I tried this javascript function before adding a.className = "year"; and everything worked properly, but then, adding it, I couldn't get the page to load neither with Safari nor Google Chrome. What I need to do is just add a class to the new a element, I tried also with a.setAttribute("class", "year"); but I got the same error: the page wouldn't load.. If someone knew how to help me I would be very grateful!

function create_timeline() {

    timeline = document.getElementById("timeline_ul");
    years = document.getElementsByClassName("year");
    for (i=0;i<years.length;i++) {
        if (years.item(i).nodeType==1) {
            year = years.item(i).innerHTML;
            a = document.createElement("a");
            a.innerHTML = year;
            a.className = "year";
            timeline.appendChild(a);
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2025-01-14 06:09:14

但我遇到了同样的错误:页面无法加载..

问题是,您的代码在无限循环中运行。原因是:

  1. 通过下面的调用,您已经获取了类为“year”的所有元素。返回的集合是动态集合

    years = document.getElementsByClassName("year");
    
  2. 在迭代集合时,您将创建新的锚元素,并将它们设置为“year”类。

  3. 当您将它们附加到文档时,它们将自动添加到第一步创建的集合中,因为它是一个动态集合,因为新元素与集合的“主题”(此处为“年份”类)匹配。

为了避免无限循环,你有很多可能性。

  1. 将长度属性缓存在变量中

    var i=0, len = 年.length;i

但使用这种方法,您无法确定新元素是否附加到集合中或以某种“逻辑”或“随机”顺序添加。

  1. 将集合转换为 JavaScript 数组

    years = document.getElementsByClassName("year");
    年 = Array.prototype.slice.call(年, 0);
    
  2. 或使用 document.querySelectorAll,它返回元素的静态集合。

    years = document.querySelectorAll('.year');
    

我会采用方法#2 或#3。

but I got the same error: the page wouldn't load..

the problem is, your code runs in a infinite loop. The reason for that is:

  1. With the call below you've fetched all elements with class "year". The collection that is returned is a dynamic collection

    years = document.getElementsByClassName("year");
    
  2. while iterating through the collection, you are creating new anchor elements, and setting them the "year" class.

  3. When you append them to the document, they will be automagically added to the collection created 1st step, because it is a dynamic collection, because new elements match the "subject" (here the class "year") of the collection.

to avoid infinite loops, you have many possibilities.

  1. cache the length property in a variable

    var i=0, len = years.length;i<len;i++
    

but with this approach, you cannot be sure whether new elements are appended to the collection or added in some "logical" or "random" order.

  1. Convert the collection to a JavaScript array

    years = document.getElementsByClassName("year");
    years = Array.prototype.slice.call(years, 0);
    
  2. or use document.querySelectorAll, which returns a static collection of elements.

    years = document.querySelectorAll('.year');
    

I would go for approach #2 or #3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文