簡體   English   中英

如何在onBlur事件中刪除DIV元素的所有子元素?

[英]How can I remove all child element of DIV element on onBlur event?

我有一個網頁,我在其中定義了一個ID為“ selectedProduct”的DIV元素,現在,當我單擊特定鏈接時,它將調用ajax請求,並用響應數據填充DIV元素。 但是我要求當onBlur事件發生在“ selectedProduct” DIV上時,它應該刪除它的所有子元素。 我的代碼如下:

<script type="text/javascript">
function getPage(event,id)
{
    event.preventDefault();
    var page = document.getElementById(id).id + ".html";
    var dataString = page               

    $.ajax(
    {
        type: "POST",
        url: page,
        data: dataString,
        cache: false,
        success: function(result)
        {
            var result=result;

            $("#selectedProduct").html(result);
            //  location.replace("index1.html");                                                
        }
    });
}

function focOFF(id)
{
 // I don't have any idea what code should i write here which can remove all child of DIV element....
}
</script>

<body>
<a href="#selectedProduct" style="text-decoration:none" id="solar_power_plant" target="new" onClick="getPage(event,this.id)" class="scroll">Solar Power Plant</a>

<div id="selectedProduct" onBlur="focOFF(this.id)">
</div>

</body>

使用以下解決方案獲取所有子元素並將其刪除,

function focOFF(id)
{
    $( "#"+id ).children().remove();
}

在模糊函數中使用empty(函數。 刪除所選元素的所有子節點

function focOFF(id)
{
  $('#'+id).empty();
}
document.getElementById(id).innerHTML = '';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM