简体   繁体   English

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

[英]About jQuery append() and how to check if an element has been appended

I made a very simple button click event handler , I would like to have <p> element be appended when button clicked, you can check my code here : 我做了一个非常简单的按钮点击事件处理程序 ,我想在点击按钮时附加<p>元素,你可以在这里查看我的代码

<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>");
});

I have two questions to ask: 我有两个问题要问:

1, why my .append() does not work as I expected (that's append the <p> element) 1,为什么我的.append()不能按预期工作(这是附加<p>元素)

2. in jQuery, how to check if some element is already appended? 2.在jQuery中,如何检查是否已经附加了一些元素? For example how to check if <p id="other"> has already appended in my case? 例如,如何检查<p id="other">是否已经附加在我的案例中?

-------------------- update ------------------------------------------- --------------------更新----------------------------- --------------

Please check my updated code here . 在此处查看我的更新代码

So, only the 2nd question remains... 所以,只有第二个问题仍然存在......

  1. You are using mootools and not jQuery. 你正在使用mootools而不是jQuery。

  2. To check if your element exists 检查您的元素是否存在

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

So if you do not want to append the element twice: 因此,如果您不想将元​​素追加两次:

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

Or, you can use the .one(function) [ doc ]: 或者,您可以使用.one(function) [ doc ]:

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

1) jsFiddle loads in MooTools by default, you need to include jQuery for this to work. 1)默认情况下,jsFiddle在MooTools中加载,你需要包含jQuery才能工作。 There is not a reason in the world why that example wouldn't work. 世界上没有理由为什么这个例子不起作用。 (Assuming that the $ is actually mapped to the jQuery object, that is.) (假设$实际上映射到jQuery对象,那就是。)

2) You can check the nextSibling of a DOMElement , or use the next() jQuery method, like so: 2)您可以检查DOMElementnextSibling ,或使用next() jQuery方法,如下所示:

if(!$('#something').next().length) {
   //no next sibling.
}

检查元素是否已经附加:

alert($(this).parent().length?'appended':'not appended');

http://jsfiddle.net/j36ye/17/ http://jsfiddle.net/j36ye/17/

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

Or 要么

var other_appended = false;

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

How to check if an element exists: 如何检查元素是否存在:

if($("p#other").length > 0) {
    // a p element with id "other" exists
}
else {
    // a p element with id "other" does not exist
}

Your fiddle is using the moo tools framework. 你的小提琴正在使用moo工具框架。 Change it to use the jquery framework on the left and it works. 将其更改为使用左侧的jquery框架,它可以正常工作。 See http://jsfiddle.net/KxGsj/ http://jsfiddle.net/KxGsj/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM