JS从孩子内部获取孩子的父母ID

时间:2018-08-15 07:30:34

标签: javascript

我有这3个div

当您点击那个孩子的孩子时,是否可以获取ID <?php $serverName = "serverName\\sqlexpress"; //serverName\instanceName $connectionInfo = array( "Database"=>"epromis_test", "UID"=>"php_test", "PWD"=>"phptest&commat123"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.<br />"; }else{ echo "Connection could not be established.<br />"; die( print_r( sqlsrv_errors(), true)); } ?> ,如果可以,怎么办?

parent
function getParentId(el) {
  //get the id "parent"
}

2 个答案:

答案 0 :(得分:1)

您可以使用Node.parentNode

function getParentId(el){
   var p = el.parentNode.parentNode;
   console.log(p.id);
} 
<div id="parent">
    <div class="child">
        <div class="child-of-child" onClick="getParentId(this)">
        <!-- some code here -->
        Click
        </div>
    </div>
</div>

答案 1 :(得分:1)

您可以在点击功能内使用el.parentElement

function getParentId(el){
   var id = el.parentElement.parentElement.id;
   console.log(id);
}
<div id="parent">
    <div class="child">
        <div class="child-of-child" onClick="getParentId(this)">
        click
        </div>
    </div>
</div>