gpt4 book ai didi

javascript - 关于 jQuery append() 以及如何检查元素是否已被追加

转载 作者:可可西里 更新时间:2023-11-01 02:49:33 28 4
gpt4 key购买 nike

我做了一个非常简单的按钮点击事件处理器,我想要<p>单击按钮时添加元素,您可以检查my code here :

<div id="wrapper">
<input id="search_btn" value="Search" type="button">
</div>
$("#search_btn").click(function(){
$("#wrapper").append("<p id='other'>I am here</p>");
});

我有两个问题要问:

1, 为什么我的 .append()没有像我预期的那样工作(附加 <p> 元素)

2. 在 jQuery 中,如何检查某个元素是否已经附加?例如如何检查 <p id="other">已经附加在我的案例中了吗?

--------------------更新-------------------- --------------------

请查看my updated code here .

所以,只剩下第二个问题了......

最佳答案

  1. 您使用的是 mootools 而不是 jQuery。

  2. 检查你的元素是否存在

if($('#other').length > 0)

因此,如果您不想将元​​素追加两次:

$("#search_btn").click(function() {
if($('#other').length == 0) {
$("#wrapper").append("<p id='other'>I am here</p>");
}
});

或者,您可以使用 .one(function)[ doc ]:

$("#search_btn").one('click', function() {
$("#wrapper").append("<p id='other'>I am here</p>");
});

关于javascript - 关于 jQuery append() 以及如何检查元素是否已被追加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7951505/

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