检查元素是否是另一个元素的子元素 [英] Check if element is child of another element

查看:85
本文介绍了检查元素是否是另一个元素的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题...

在开始循环遍历< div>的所有子元素之前我想询问是否有更好的方法来检查元素是否是另一个元素的子元素?


我以为我找到了一个方法(包含(pchild)方法)但是没有找不到这种方法的任何文档,显然它没有像我想象的那样工作。


感谢您的时间


-Frinny

解决方案

只需查看 nodeType 属性(元素为3)


如果你想使用NodeLists,你也可以使用 element.getElementsByTagName(" *");


如果元素是另一个元素的子元素



元素始终是另一个元素的子元素(< html>元素除外)


我会查看nodeType属性....没有看到它被列为< div>的属性虽然元素。


我只是希望有像document.getElementById那样检查我所拥有的元素是否是< div>的孩子......


谢谢宿舍,


-Frinny


嗯,不知道nodeType属性将如何帮助我。


我想我应该提供更多我正在尝试做的细节。


我正在使用ASP.NET UpdatePanels。它在ASP.NET中的工作方式是,放在UpdatePanel中的任何内容都参与到服务器的Ajax /异步请求中。当请求返回时,它被剥离以仅返回刷新页面上该部分所需的数据(UpdatePanels呈现为< div> s,因此它只刷新< div>中的内容,即UpdatePanel)。


我有一个元素只有在它参与其中一个Ajax请求时才需要刷新...只有当它在一个活动的UpdatePanel中时才会刷新。 />

但是现在只要有任何ajax请求返回浏览器,就会执行更新元素的代码。


我需要一种只执行的方法如果元素处于活动状态,则此代码为UpdatePanel,需要更新。


我有需要更新的元素的ID ...我可以访问所涉及的所有UpdatePanel ID的列表在Ajax请求中。


我只需要弄清楚该元素是否是列表中某个UpdatePanel的子元素,这样我就可以调用更新元素的方法。 br />

我已经尝试了很多东西,现在我开始考虑循环遍历UpdatePanel列表中的所有子元素,以确定它是否在那里....


我知道必须有一个更好的方法!


-Frinny


I have a quick question...

Before I start looping through all of the child elements of a <div> I wanted to ask if there is a better way to check if an element is a child of another element?

I thought I found a method (the contains(pchild) method) but didn''t find any documentation for this method and apparently it doesn''t work the way I thought it did.

Thanks for your time

-Frinny

解决方案

just check out the nodeType property (it?s 3 for elements)

if you want to work with NodeLists, you can also use element.getElementsByTagName("*");

if an element is a child of another element

an element is always a child of another element (except the <html> element)


I''ll check out the nodeType property....didn''t see it listed as a property of the <div> element though.

I just wish there was something like the document.getElementById that checks if the element I have is a child of the <div>...

Thanks Dorm,

-Frinny


Hmm, not sure how the nodeType property is going to help me.

I guess I should provide more details on what I''m trying to do.

I am using ASP.NET UpdatePanels. The way it works in ASP.NET is that whatever''s placed in an UpdatePanel takes part in Ajax/asynchronous requests to the server. When the request returns it''s stripped down to only return the data required to refresh that section on the page (UpdatePanels are rendered as <div>s so it only refreshes the content that is in the <div> that is the UpdatePanel).

I have an element that needs to be refreshed only if it partakes in one of the Ajax requests...only if it''s in an active UpdatePanel.

But right now whenever any ajax request comes back to the browser the code to update the element is executed.

I need a way to only execute this code if the element is in the "active" UpdatePanel and needs to be updated.

I have the ID of the element that needs to be updated...and I have access to a list of all of the UpdatePanel IDs that were involved in the Ajax request.

I just need to figure out if the element is a child of one of the UpdatePanels in the list so that I can call the method that updates the element.

I''ve tried a whole bunch of things and now I''m starting think about looping through all of the child elements in the list of UpdatePanels to determine if it''s in there....

I know there has to be a better way though!

-Frinny


这篇关于检查元素是否是另一个元素的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆