Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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 遍历DOM以确定父对象的状态。根据条件返回字符串_Javascript - Fatal编程技术网

Javascript 遍历DOM以确定父对象的状态。根据条件返回字符串

Javascript 遍历DOM以确定父对象的状态。根据条件返回字符串,javascript,Javascript,这是我的代码: var findParentByClassName = function(element, targetClass) { if (element) { element.parentElement === null; console.log("No parent found"); } if else { element.parentElement !=== targetClass; console.log("No parent fou

这是我的代码:

var findParentByClassName = function(element, targetClass) {
if (element) {
    element.parentElement === null;
        console.log("No parent found");
} if else {
    element.parentElement !=== targetClass;
        console.log("No parent found with that class name");
} else {
    var currentParent = element.parentElement;
    while (currentParent.className !== targetClass && currentParent.className !== null) {
        currentParent = currentParent.parentElement;
    }
    return currentParent;
}
};
我需要能够检查其他两种情况:

  • 检查所提供的“元素”是否有父元素。如果是,则向控制台发送字符串
  • 如果它没有找到具有提供的“targetClass”名称的父级,则让它向控制台返回不同的字符串
  • 我假设这将通过if-if-else-else函数完成,但就我的一生而言,我无法获得它。

    尝试使用此函数(代码片段下解释了代码):

    首先,您需要仔细检查是否实际获得了一个元素。否则,我们将无能为力

    然后,我们使用
    else if
    -而不是
    else
    -来检查元素是否有父元素。如果不行,我们就什么也做不了

    最后,如果有一个具有祖先的元素,则执行else分支。从这一点上,我们可以迭代地向上移动DOM树。如果我们找到一个元素,其类列表包含给定的类名,那么我们已经找到了一个匹配的祖先,并返回它。否则,我们返回null

    请注意,此代码查找具有匹配类名的祖先,该类名称不一定是直接父级。根据你的代码,我假设这就是你想要的

    现在,如果使用jQuery,这将变得容易得多:

    var findParentByClassName = function(elem, className)
    {
      return $(elem).closest("." + className);
    };
    
    这里的缺点是,您将无法记录消息 如果不存在具有正确类名的祖先,为什么找不到具有正确类名的祖先

    下面是if-else-if语句的工作原理:

    if(condition)
    {
      // code to execute if the condition is true
    }
    else if(condition2) // condition2 is only checked if the 1st one is false
    {
      // code to run if condition2 is true
    }
    else if(condition3) // you can have 0 or more else if conditions
    {
      // in practice, you can have as many as you want
      // there are limits, but you'll probably never reach them
      // unless you're trying to
    }
    else
    {
      // code to execute if none of the conditions were true
    }
    
    条件是布尔表达式。这些可以是布尔值, 相等性检查,大于/小于,是否为变量 为空/未定义,依此类推

    我建议您阅读JavaScript教程以了解更多信息。

    尝试使用此方法(代码片段下解释了代码):

    首先,您需要仔细检查是否实际获得了一个元素。否则,我们将无能为力

    然后,我们使用
    else if
    -而不是
    else
    -来检查元素是否有父元素。如果不行,我们就什么也做不了

    最后,如果有一个具有祖先的元素,则执行else分支。从这一点上,我们可以迭代地向上移动DOM树。如果我们找到一个元素,其类列表包含给定的类名,那么我们已经找到了一个匹配的祖先,并返回它。否则,我们返回null

    请注意,此代码查找具有匹配类名的祖先,该类名称不一定是直接父级。根据你的代码,我假设这就是你想要的

    现在,如果使用jQuery,这将变得容易得多:

    var findParentByClassName = function(elem, className)
    {
      return $(elem).closest("." + className);
    };
    
    这里的缺点是,您将无法记录消息 如果不存在具有正确类名的祖先,为什么找不到具有正确类名的祖先

    下面是if-else-if语句的工作原理:

    if(condition)
    {
      // code to execute if the condition is true
    }
    else if(condition2) // condition2 is only checked if the 1st one is false
    {
      // code to run if condition2 is true
    }
    else if(condition3) // you can have 0 or more else if conditions
    {
      // in practice, you can have as many as you want
      // there are limits, but you'll probably never reach them
      // unless you're trying to
    }
    else
    {
      // code to execute if none of the conditions were true
    }
    
    条件是布尔表达式。这些可以是布尔值, 相等性检查,大于/小于,是否为变量 为空/未定义,依此类推


    我建议您阅读JavaScript教程了解更多信息。

    您尝试过什么吗?请阅读。关键短语:“搜索、研究”和“解释……任何阻碍你自己解决问题的困难”。@Dekel-是的。我道歉。我粘贴了错误的代码。后来我改正了。这段代码似乎工作得很好,只要我把“if-else”注释掉,所有内容都会正确地显示在页面上。如果该段是活动的,那么我将丢失此引用的表。
    }如果其他{
    !==
    ??您将要在循环中测试
    当前父级的类,而不是
    元素。您尝试了什么吗?请阅读。关键短语:“搜索和研究”和“解释……任何阻碍您自己解决问题的困难”。@Dekel-是的。我很抱歉。我粘贴了错误的代码。我已经更正了。代码似乎工作得很好,因为只要我有“如果其他”选项,所有内容都会正确显示在页面上“已注释掉。如果该段处于活动状态,则我将丢失此引用的表。
    }否则{
    !==
    ??您将希望在循环内测试
    currentParent
    的类,而不是
    元素