Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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找出div a div是什么?_Javascript_Dhtml_Html - Fatal编程技术网

如何使用javascript找出div a div是什么?

如何使用javascript找出div a div是什么?,javascript,dhtml,html,Javascript,Dhtml,Html,我有一个页面,其中将包含4个ID为的静态div。我将添加scriptaculous可拖放效果,通过将许多动态生成的div从一个div移动到另一个div来实现交互 我需要能够找出哪些静态div是动态div。如何获取此信息?由于您计划使用Scriptaculous,因此可以通过Prototype轻松实现这一点: if ($("dynamicElement").descendantOf("staticElement")) { } 由于您计划使用Scriptaculous,因此可以通过Prototy

我有一个页面,其中将包含4个ID为的静态div。我将添加scriptaculous可拖放效果,通过将许多动态生成的div从一个div移动到另一个div来实现交互


我需要能够找出哪些静态div是动态div。如何获取此信息?

由于您计划使用Scriptaculous,因此可以通过Prototype轻松实现这一点:

if ($("dynamicElement").descendantOf("staticElement"))
{

}

由于您计划使用Scriptaculous,因此可以通过Prototype轻松实现这一点:

if ($("dynamicElement").descendantOf("staticElement"))
{

}

据我所知,您想查找已知元素的所有child(或所有child div)

在使用scriptaculous时,可以使用prototype中的childElements方法。 有关详细信息,请参阅


欢迎使用stackoverflow。

据我所知,您想查找已知元素的所有子项(或所有子项div)

在使用scriptaculous时,可以使用prototype中的childElements方法。 有关详细信息,请参阅


欢迎使用stackoverflow。

上面的评论者注意到了实现这一点的脚本式方法,但知道如何以这种方式不借助拐杖执行基本的DOM操作总是值得的。此代码未经测试,但肯定非常接近您想要使用的代码:

/**
 * Finds the ancestor div element of descendantDiv which has one of
 * the given array of ids.  If multiple div elements have with the
 * given ids are ancestors of descendantDiv, the most closely related
 * one (i.e. parent preferred to grandparent) is returned.  If no
 * ancestor of descendantDiv is a div element with one of the ids
 * given by ids, returns null.
 *
 * @param descendantDiv : HTMLDivElement
 *   the div element whose containing div with one of the provided
 *   ids is to be determined
 * @param ids : [string]
 *   array of ids which the desired ancestor div might have
 * @returns HTMLDivElement
 *   the first parent, grandparent, etc. div element of descendantDiv
 *   which has an id contained in ids
 */
function findAncestorDivWithId(descendantDiv, ids)
{
  for (var parent = descendantDiv.parentNode;
       parent;
       parent = parent.parentNode)
  {
    if (parent.nodeName.toLowerCase() !== "div")
      continue;
    for (var i = 0, sz = ids.length; i < sz; i++)
    {
      if (parent.id === ids[i])
        return parent;
    }
  }
  return null;
}
/**
*查找具有以下项之一的后代div的祖先div元素:
*给定的ID数组。如果多个div元素与
*给定ID是关系最密切的后代div的祖先
*返回一个(即父母优先于祖父母)。如果没有
*后代div的祖先是一个具有以下ID之一的div元素
*由ids给定,返回null。
*
*@param-degenantdiv:htmldevelment
*其包含div的div元素具有提供的
*ID有待确定
*@param id:[字符串]
*所需祖先div可能具有的ID数组
*@returns htmldevelment
*子代div的第一个父代、祖父母等div元素
*它的id包含在id中
*/
函数findanceStordIvHitid(后代div,id)
{
对于(var parent=genderantdiv.parentNode;
父母亲
parent=parent.parentNode)
{
if(parent.nodeName.toLowerCase()!=“div”)
继续;
对于(var i=0,sz=ids.length;i
上面的评论者指出了实现这一点的脚本式方法,但知道如何以这种方式不借助拐杖执行基本的DOM操作总是值得的。此代码未经测试,但肯定非常接近您想要使用的代码:

/**
 * Finds the ancestor div element of descendantDiv which has one of
 * the given array of ids.  If multiple div elements have with the
 * given ids are ancestors of descendantDiv, the most closely related
 * one (i.e. parent preferred to grandparent) is returned.  If no
 * ancestor of descendantDiv is a div element with one of the ids
 * given by ids, returns null.
 *
 * @param descendantDiv : HTMLDivElement
 *   the div element whose containing div with one of the provided
 *   ids is to be determined
 * @param ids : [string]
 *   array of ids which the desired ancestor div might have
 * @returns HTMLDivElement
 *   the first parent, grandparent, etc. div element of descendantDiv
 *   which has an id contained in ids
 */
function findAncestorDivWithId(descendantDiv, ids)
{
  for (var parent = descendantDiv.parentNode;
       parent;
       parent = parent.parentNode)
  {
    if (parent.nodeName.toLowerCase() !== "div")
      continue;
    for (var i = 0, sz = ids.length; i < sz; i++)
    {
      if (parent.id === ids[i])
        return parent;
    }
  }
  return null;
}
/**
*查找具有以下项之一的后代div的祖先div元素:
*给定的ID数组。如果多个div元素与
*给定ID是关系最密切的后代div的祖先
*返回一个(即父母优先于祖父母)。如果没有
*后代div的祖先是一个具有以下ID之一的div元素
*由ids给定,返回null。
*
*@param-degenantdiv:htmldevelment
*其包含div的div元素具有提供的
*ID有待确定
*@param id:[字符串]
*所需祖先div可能具有的ID数组
*@returns htmldevelment
*子代div的第一个父代、祖父母等div元素
*它的id包含在id中
*/
函数findanceStordIvHitid(后代div,id)
{
对于(var parent=genderantdiv.parentNode;
父母亲
parent=parent.parentNode)
{
if(parent.nodeName.toLowerCase()!=“div”)
继续;
对于(var i=0,sz=ids.length;i
嗯,我不想找到一个div的所有元素,因为我会让多个用户从同一个数据库拖放,所以读取所有元素然后写入数据库可能会撤消某些人的更改。听起来(到目前为止给出的选项中)最好的选择是4个if语句,与第一个答案中的语句类似。谢谢大家!(还有,我不想找到一个div的所有元素,因为我会让多个用户从同一个数据库拖放,所以读取所有元素,然后写入数据库,可能会撤消某些人的更改。这听起来是(到目前为止给出的选项中)最好的选择。)是4个if语句,与第一个答案中的语句相似。谢谢大家!(还有,哇!谢谢你,我想我需要去w3schools查看一份关于所有内置DOM能力的大备忘单。我刚刚开始对Javascript沾沾自喜。哇!谢谢你,我想我需要去w3schools查看一份关于所有内置DOM能力的大备忘单。我刚刚开始对Javascript沾沾自喜。