简体   繁体   English

通过子元素获取Div

[英]Get Div by Child Element

I have the following code 我有以下代码

<div id="faqEntries">
    <p:inputText disabled="true" class="telDialogField"></p:inputText>
    <br/>
    <p:inputTextarea disabled="true" class="telDialogField"></p:inputTextarea>
    <br/>
    <p:button value="In Ticket übernehmen" class=" telDialogButton"></p:button>
</div>

this part is for showing faq entries from a database. 这部分用于显示数据库中的常见问题。 This part is nested inside of a ui:repeat. 这部分嵌套在ui:repeat内部。

what I want to do now is get the surrounding Div when I click the button, so that I can get the Textarea afterwards and copy this text to an other TextArea. 我现在想做的是单击按钮时获取周围的Div,以便以后可以获取Textarea并将此文本复制到另一个TextArea。 Normaly I would just try to do this using id Tags but this does not seem to work with the ui:repeat. 通常,我只是尝试使用id标签来执行此操作,但这似乎不适用于ui:repeat。

I know how I can copy the content into another textarea but not how to get the first textarea. 我知道如何将内容复制到另一个文本区域,但不知道如何获取第一个文本区域。

There are two ways. 有两种方法。

Pure Javascript: 纯Javascript:

var pDoc = document.getElementById("yourDiv");
var parentDiv = pDoc.parentNode;

Using Jquery: 使用jQuery:

var parentDiv = $("#yourDiv").parent()

You can get the parent element by using .parentElement property. 您可以使用.parentElement属性获取父元素。

<div id="faqEntries">
    <p:inputText disabled="true" class="telDialogField"> 
    </p:inputText>
    <br/>
    <p:inputTextarea disabled="true" class="telDialogField"> 
    </p:inputTextarea>
    <br/>
    <p:button onclick="foo(this.parentElement);" value="In Ticket übernehmen" class=" telDialogButton"> 
    </p:button>
</div>

The foo() function can then do whatever you wish with the parent element. 然后,foo()函数可以使用父元素执行任何您想做的事情。

function foo(parent){
    alert(parent.nodeName);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM