Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有人能帮我理解下面的代码吗? 函数talksAbout(节点,字符串){ 如果(node.nodeType==document.ELEMENT_node){ 对于(var i=0;i-1; } } console.log(talksAbout(document.body,“book”));_Javascript - Fatal编程技术网

Javascript 有人能帮我理解下面的代码吗? 函数talksAbout(节点,字符串){ 如果(node.nodeType==document.ELEMENT_node){ 对于(var i=0;i-1; } } console.log(talksAbout(document.body,“book”));

Javascript 有人能帮我理解下面的代码吗? 函数talksAbout(节点,字符串){ 如果(node.nodeType==document.ELEMENT_node){ 对于(var i=0;i-1; } } console.log(talksAbout(document.body,“book”));,javascript,Javascript,我在第234页的雄辩Javascript中找到了这段代码。如果有人逐行解释的话,这将非常有用。DOM由不同类型的节点组成。Body、Div等是元素节点,而input:text、text Area是文本节点等等。函数talksAbout递归地遍历给定“body”元素节点的子节点,直到找到值为“book”的文本节点。此代码在页面中搜索单词“book”,如果找到,则返回true。我认为代码很容易理解和阅读。但你需要知道这一点才能理解(在页面底部) 函数talksAbout(node,string){/

我在第234页的雄辩Javascript中找到了这段代码。如果有人逐行解释的话,这将非常有用。

DOM由不同类型的节点组成。Body、Div等是元素节点,而input:text、text Area是文本节点等等。函数talksAbout递归地遍历给定“body”元素节点的子节点,直到找到值为“book”的文本节点。

此代码在页面中搜索单词“book”,如果找到,则返回true。我认为代码很容易理解和阅读。但你需要知道这一点才能理解(在页面底部)

函数talksAbout(node,string){//声明了一个名为talksAbout的函数,它接受两个参数:node和string
if(node.nodeType==document.ELEMENT\u node){//检查传入的节点是否为“ELEMENT\u node”-依此类推
for(var i=0;i-1;//如果传入的字符串在此文本节点中的某个位置,则返回true
}//如果是块,则结束
}//结束函数块
console.log(talksAbout(document.body,“book”);//执行函数,传入body元素和字符串“book”,将结果记录到控制台

基本上,此函数查看页面上的文本中是否存在“book”一词-因此
不会被视为匹配项

它检查文档是否包含“book”一词。我知道这一点,但无法理解整个代码
function talksAbout(node, string) { 
    if (node.nodeType == document.ELEMENT_NODE) { 
        for (var i = 0; i < node.childNodes.length; i++) { 
            if (talksAbout(node.childNodes[i], string))
                return true;
        } 
        return false; 
    } else if (node.nodeType == document.TEXT_NODE) { 
        return node.nodeValue.indexOf(string) > -1;
    } 
} 
console.log(talksAbout(document.body, "book"));
function talksAbout(node, string) { // declares a function called talksAbout, which takes two parameters: node and string
    if (node.nodeType == document.ELEMENT_NODE) { // checks if the passed in node is an "ELEMENT_NODE" - <body><div><span>etc
        for (var i = 0; i < node.childNodes.length; i++) { // loops through all the child nodes of the passed in node
            if (talksAbout(node.childNodes[i], string)) // recursively calls the function on each node
                return true; // returns true if the function returns true
        } // ends the for loop block
        return false; // returns false
    } else if (node.nodeType == document.TEXT_NODE) { // if the node is a TEXT node
        return node.nodeValue.indexOf(string) > -1; // returns true if the passed in string is somewhere in this text node
    } // ends ths else if block
} // ends the function block
console.log(talksAbout(document.body, "book")); // execute the function, passing in the body element, and a string "book", logging the result to the console