小编Poc*_*ets的帖子

getElementsByClassName究竟是如何在Chrome中运行的?特别是wrt NodeLists和DOM

以下所有结果均使用Google Chrome v36及其控制台获得.

在调试Wordpress插件时,我发现运行这个小Javascript片段

console.log(document.getElementsByClassName("switch-tmce"))
console.log(document.getElementsByClassName("switch-tmce").length)
Run Code Online (Sandbox Code Playgroud)

将记录以下内容(在页面加载完成后展开):

[item: function, namedItem: function]
    0: a#ninja_forms_field_10-tmce.hide-if-no-js.wp-switch-editor.switch-tmce
    1: a#ninja_forms_field_15-tmce.hide-if-no-js.wp-switch-editor.switch-tmce
    length: 2
    ninja_forms_field_10-tmce: a#ninja_forms_field_10-tmce.hide-if-no-js.wp-switch-editor.switch-tmce
    ninja_forms_field_15-tmce: a#ninja_forms_field_15-tmce.hide-if-no-js.wp-switch-editor.switch-tmce
    __proto__: HTMLCollection
0
Run Code Online (Sandbox Code Playgroud)

如果我调整了片段以等待DOM完成加载,如下所示:

window.addEventListener("DOMContentLoaded", function() {
    console.log(document.getElementsByClassName("switch-tmce"))
    console.log(document.getElementsByClassName("switch-tmce").length)
}, false);
Run Code Online (Sandbox Code Playgroud)

然后它将记录以下内容(在页面加载完成后展开):

[a#ninja_forms_field_10-tmce.hide-if-no-js.wp-switch-editor.switch-tmce, a#ninja_forms_field_15-tmce.hide-if-no-js.wp-switch-editor.switch-tmce, ninja_forms_field_10-tmce: a#ninja_forms_field_10-tmce.hide-if-no-js.wp-switch-editor.switch-tmce, ninja_forms_field_15-tmce: a#ninja_forms_field_15-tmce.hide-if-no-js.wp-switch-editor.switch-tmce, item: function, namedItem: function]
    0: a#ninja_forms_field_10-tmce.hide-if-no-js.wp-switch-editor.switch-tmce
    1: a#ninja_forms_field_15-tmce.hide-if-no-js.wp-switch-editor.switch-tmce
    length: 2
    ninja_forms_field_10-tmce: a#ninja_forms_field_10-tmce.hide-if-no-js.wp-switch-editor.switch-tmce
    ninja_forms_field_15-tmce: a#ninja_forms_field_15-tmce.hide-if-no-js.wp-switch-editor.switch-tmce
    __proto__: HTMLCollection
2
Run Code Online (Sandbox Code Playgroud)

我无法理解的是这里到底发生了什么 - 特别是,为什么length属性只能在DOM加载 "正确"返回.我发现了这个解释:

可能是,在调用getElementsByTagName时,不存在任何输入元素,但由于NodeLists是动态的,因此当文档加载时,元素将包含所有28个输入.

但是我正在读这个说getElementsByTagName解析NodeList直到它可以解析DOM,并且只能length在解析DOM时返回属性,这对我来说似乎不对,因为它仍然具有有限的可数元素.

此外,还有[item:function, namedItem:function]改变的问题,[a.someClass.someOtherClass, a.someclass.someOtherClass]上面没有解释.

因此我的问题是:尽管原型保持不变,但是在DOM加载之后getElementsByClassName,该length …

javascript google-chrome getelementsbyclassname

3
推荐指数
1
解决办法
1506
查看次数

C++评估顺序

我试图弄清楚C++ 11规范中是否有任何内容.以下代码的预期行为(此处GitHub链接):

struct Scalar {
    int data;

    Scalar(int x) : data(x) {}

    int get() {
        return data;
    }

    Scalar &square() {
        scale(data);
        return *this;
    }

    void scale(int rhs) {
        data *= rhs;
    }
};

int main() {
    Scalar v(3);

    v.square().scale(v.get());

    return v.data;
}
Run Code Online (Sandbox Code Playgroud)

这主要是因为发现它在g++和之间做了不同的事情clang++:

$ g++ --version
g++ (GCC) 6.2.1 20160830
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; …
Run Code Online (Sandbox Code Playgroud)

c++ g++ c++11 clang++

3
推荐指数
1
解决办法
114
查看次数

Python字符串连接 - 意外行为

我正在获得绝对最奇怪的 Python行为,我不能为我的生活弄明白.运气好的话,有人在这里知道发生了什么.

我循环使用以下方法:

def request_hiscores_for(name):
  print '+++++ request_hiscores_for() running +++++'+name
  print type(name)
  print '  Requesting hiscores for $name using request_hiscores_for()'.replace('$name',name)
  print '  Requesting hiscores for '+name+' using request_hiscores_for()'
  print '  Requesting hiscores for $name using request_hiscores_for()'.replace('$name','BLA')
  print '----- request_hiscores_for() ending ------\n'
Run Code Online (Sandbox Code Playgroud)

并按以下方式获得输出:

+++++ request_hiscores_for() running +++++10032
<type 'str'>
 using request_hiscores_for()32
 using request_hiscores_for()32
  Requesting hiscores for BLA using request_hiscores_for()
----- request_hiscores_for() ending ------

+++++ request_hiscores_for() running +++++123 Lvs 2 Go
<type 'str'>
 using request_hiscores_for() Lvs 2 Go
 using request_hiscores_for() Lvs …
Run Code Online (Sandbox Code Playgroud)

python string

1
推荐指数
1
解决办法
116
查看次数