简体   繁体   English

为什么我们在我指定的代码中写 [0] ?

[英]why do we write [0] in the code which i specified?

why do we write (item.classList[0] === "complete-btn")我们为什么要写(item.classList[0] === "complete-btn")

like what is "item.classList[0]" and why is it 0 [0] here?比如什么是"item.classList[0]" ,为什么这里是 0 [0]?

if(item.classList[0] === "complete-btn"){
        const todo = item.parentElement;
        todo.classList.toggle("completed"); 
     }  
}

classList is a DOMTokenList of the class names on an element. classList是元素上DOMTokenList名称的 DOMTokenList。 You can index into the list the way you do with arrays to access the individual tokens (classes, in this case) in the list.您可以像使用 arrays 一样对列表进行索引,以访问列表中的各个令牌(在本例中为类)。 For instance, if you have class="abc" , then .classList[0] is "a" , .classList[1] is "b" , and .classList[2] is "c" .例如,如果你有class="abc" ,那么.classList[0]"a".classList[1]"b".classList[2]"c" But , keep reading.但是,请继续阅读。

The author of the code in the question decided it made sense to see if the first class in the list was complete-btn .问题中代码的作者认为查看列表中的第一个class 是否为complete-btn是有意义的。 This is poor practice, because the order of classes in the class list is not important and it can change as classes are added and removed from the list.¹ For that reason, it would be better written with contains :这是一种不好的做法,因为 class 列表中的类顺序并不重要,并且可以随着从列表中添加和删除类而改变。¹因此,最好使用contains编写:

if (item.classList.contains("complete-btn")) {

The original code would fail for an element with class="example complete-btn" , whereas the code using contains doesn't:对于带有class="example complete-btn"的元素,原始代码会失败,而使用contains的代码不会:

 const item = document.querySelector(".example"); console.log(item.classList[0] === "complete-btn"); // false, [0] is "example" console.log(item.classList.contains("complete-btn")); // true
 <div class="example complete-btn"></div>


¹ "This is poor practice, because the order of classes in the class list is not important and it can change as classes are added and removed from the list." ¹ “这是一种不好的做法,因为 class 列表中的类顺序并不重要,并且随着从列表中添加和删除类,它可能会发生变化。” Granted, if complete-btn is the first class listed in the class attribute of the HTML and that class is never removed via .classList.remove or .classList.toggle , it will remain the first in the list; Granted, if complete-btn is the first class listed in the class attribute of the HTML and that class is never removed via .classList.remove or .classList.toggle , it will remain the first in the list; still, it's poor practice to rely on it, not least because someone maintaining the HTML could add another class at the beginning, not realizing the JavaScript was relying on order — because in general, there is no order to the classes in class . still, it's poor practice to rely on it, not least because someone maintaining the HTML could add another class at the beginning, not realizing the JavaScript was relying on order — because in general, there is no order to the classes in class .

item.classlist returns a DOMTokenList Object which is like an array and has all classes in it. item.classlist返回一个 DOMTokenList Object ,它就像一个数组,其中包含所有类。 Here we are accessing its first class from the array that's why 0.在这里,我们从数组中访问它的第一个 class,这就是为什么为 0。

为什么使用 AJAX 返回的 Javascript 代码未写入指定的<div> ,但它会替换整个页面?</div><div id="text_translate"><p> 我用 AJAX(使用 jQuery)请求一个包含 Javascript function 调用 Apache+PHP 服务器的标签。 这是 HTML 和 AJAX 代码:</p><pre> &lt;.DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1:1//EN" "http.//www.w3.org/TR/xhtml11/DTD/xhtml11:dtd"&gt; &lt;html xmlns="http.//www.w3:org/1999/xhtml"&gt; &lt;head&gt; &lt;script type="text/javascript" src="http.//localhost/LivingLab/javascript/jquery.min:js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="javascript. void(0)" onclick=" $:post( 'http.//localhost/test,php': // server target page {some_var, 'abc'}. // data to be sent via POST method function(data) { // JS callback function $('#container');html(data); // innerHTML of &lt;div id="container"&gt; will be replaced } ) "&gt;Click me!&lt;/a&gt; &lt;div id="container"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> AJAX 请求具有以下 PHP 代码的页面“http://localhost/test.php”:</p><pre> &lt;?php echo '&lt;script type="text/javascript"&gt; document.write("Javascript output"); &lt;/script&gt;'; ?&gt;</pre><p> 当我点击链接时,在 AJAX 请求之后,整个页面被“Javascript 输出”替换,而不是仅在 id 为“容器”的 div 标记内写入。</p><p> 如果 PHP 服务器脚本写入其他内容,而不是像这样的脚本标记:</p><pre> &lt;?php echo 'text'?&gt;</pre><p> AJAX 调用行为正常,通过将 div 替换为 id“容器”而不是整个页面。</p><p> 我尝试使用 Wireshark 调查此页面传输的 HTTP 数据,它似乎不是 HTTP 问题,因为服务器响应正是它应该的样子:</p><pre> &lt;script type="text/javascript"&gt; document.write("Javascript output"); &lt;/script&gt;</pre><p> 在没有 AJAX 的情况下运行 Javascript 代码不会替换整个页面,仅当使用 AJAX 返回脚本标记时才会发生这种情况。 A 注意到 Firefox 3 和 5 以及 Google Chrome 中的相同行为。</p><p> 为什么会这样?</p></div> - Why a Javascript code returned with AJAX doesn't write in a specified <div>, but it replaces the whole page?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写这样的onclick代码以使我能够进行分析? - How do I write such an onclick code which enable me to analyse? 我们为什么要写窗口。 - Why do we write window.? 为什么这是有效的代码,我们称之为什么 - Why this is valid code and what do we call it 为什么我们在XMLHttpRequest中编写send()之前编写onload()函数 - Why do we write onload() function before we write send() in XMLHttpRequest 当我可以在客户端代码中逐字编写规则时,有人可以向我解释为什么我们需要 Firebase 安全规则吗? - Can someone please explain to me why we need Firebase security rules when I can literally write rules in my client code? 如何在浏览器中编写代码执行器 - How do I write a code executor in browser 为什么我们要在不同的文件中重构 Node.js 代码 - Why do we refactor Node.js code in different files 为什么我们要以 express 形式返回状态码和响应? - Why do we return the status code and response in express? 如何循环指定代码区域以编译 Javascript 中的数据? - How do I loop a specified region of code to compile data in Javascript? 为什么使用 AJAX 返回的 Javascript 代码未写入指定的<div> ,但它会替换整个页面?</div><div id="text_translate"><p> 我用 AJAX(使用 jQuery)请求一个包含 Javascript function 调用 Apache+PHP 服务器的标签。 这是 HTML 和 AJAX 代码:</p><pre> &lt;.DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1:1//EN" "http.//www.w3.org/TR/xhtml11/DTD/xhtml11:dtd"&gt; &lt;html xmlns="http.//www.w3:org/1999/xhtml"&gt; &lt;head&gt; &lt;script type="text/javascript" src="http.//localhost/LivingLab/javascript/jquery.min:js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="javascript. void(0)" onclick=" $:post( 'http.//localhost/test,php': // server target page {some_var, 'abc'}. // data to be sent via POST method function(data) { // JS callback function $('#container');html(data); // innerHTML of &lt;div id="container"&gt; will be replaced } ) "&gt;Click me!&lt;/a&gt; &lt;div id="container"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> AJAX 请求具有以下 PHP 代码的页面“http://localhost/test.php”:</p><pre> &lt;?php echo '&lt;script type="text/javascript"&gt; document.write("Javascript output"); &lt;/script&gt;'; ?&gt;</pre><p> 当我点击链接时,在 AJAX 请求之后,整个页面被“Javascript 输出”替换,而不是仅在 id 为“容器”的 div 标记内写入。</p><p> 如果 PHP 服务器脚本写入其他内容,而不是像这样的脚本标记:</p><pre> &lt;?php echo 'text'?&gt;</pre><p> AJAX 调用行为正常,通过将 div 替换为 id“容器”而不是整个页面。</p><p> 我尝试使用 Wireshark 调查此页面传输的 HTTP 数据,它似乎不是 HTTP 问题,因为服务器响应正是它应该的样子:</p><pre> &lt;script type="text/javascript"&gt; document.write("Javascript output"); &lt;/script&gt;</pre><p> 在没有 AJAX 的情况下运行 Javascript 代码不会替换整个页面,仅当使用 AJAX 返回脚本标记时才会发生这种情况。 A 注意到 Firefox 3 和 5 以及 Google Chrome 中的相同行为。</p><p> 为什么会这样?</p></div> - Why a Javascript code returned with AJAX doesn't write in a specified <div>, but it replaces the whole page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM