gpt4 book ai didi

javascript - 使用 JavaScript 确定/查找 HTML DIV 元素是否为空或未使用

转载 作者:太空狗 更新时间:2023-10-29 15:35:27 27 4
gpt4 key购买 nike

<分区>

还有另一个帖子可能已经回答了我的问题。为了找到那个帖子,我需要搜索 child nodeschildren .但问题是,我当时不知道什么是子节点或 HTML 子节点。

这不是 jQuery 问题。

我的问题是:如何确定 HTML 元素是否为空或未使用?例如:

<div id="UserRegistration"></div>

以上<div>元素没有内容。与此相反:

<div id="UserRegistration">
<label>Password:</label>
<input type="password" id="password" placeholder="Your New Password" required maxlength="32"/>
<label>Verify Password:</label>
<input type="password" name="ReEnterPassword" id="reenterpassword" required maxlength="32"/><br/>
</div>

我故意将元素留空,然后在用户单击菜单选项卡时将 HTML 注入(inject)其中。但如果用户多次单击菜单项,我不想多次注入(inject)相同的 HTML。为避免这种情况,我希望代码确定该元素是否已被注入(inject)内容。我想使用 Web API 接口(interface)和 JavaScript。使用 .innerHTML 添加内容

document.getElementById('UserRegistration').innerHTML=VariableWithRetreivedContent;

当用户点击 Register菜单选项卡,功能运行。我试过使用它来确定是否已经添加了内容,但它不起作用:

function goToRegistration(ElmtToGoTo, FileToGet) {
//Use getElementById to get info from the ElmtToGoTo DIV
// and put info into the el variable
var el = document.getElementById(ElmtToGoTo);
alert(el.value);
// If element is already filled quit
if (el.value === undefined || el.value === "")
{
/To Do: Quit Here
};

//To Do: Get content and put it into DIV element
};

我得到的是undefined DIV 中是否包含任何内容。我如何测试 <div> 是否元素已经被注入(inject)了内容?

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