gpt4 book ai didi

javascript - 如何在 onBlur 事件上删除 DIV 元素的所有子元素?

转载 作者:行者123 更新时间:2023-11-28 18:23:52 25 4
gpt4 key购买 nike

我有一个网页,我在其中定义了一个 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();
}

关于javascript - 如何在 onBlur 事件上删除 DIV 元素的所有子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39490560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com