简体   繁体   English

附加然后使用jQuery删除元素

[英]Append then remove element with jQuery

Trying to append an element on mouseenter then remove the same element on mouseleave. 尝试在mouseenter上追加元素,然后在mouseleave上删除相同的元素。

$(#foo).mouseenter(function (){
    $(this).append(#bar);
});
$(#foo).mouseleave(function (){
    $(this).remove(#bar);
});

Doesn't work, am I doing something wrong? 不起作用,我做错什么了吗?

Not quoting string literals appears to be the most obvious thing. 不引用字符串文字似乎是最明显的事情。 Have you tried: 你有没有尝试过:

$("#foo").mouseenter(function (){
    $(this).append("#bar");
});
$("#foo").mouseleave(function (){
    $(this).remove("#bar");
});

jQuery's element-selector function (ie $() ) expects a string containing a CSS-style selector, like ".someClass" or "#someId" or "div span.label" . jQuery的element-selector函数(即$() )期望包含CSS样式选择器的字符串,例如".someClass""#someId""div span.label" Passing those things without quotes around them is a syntax error. 传递那些没有引号的东西是语法错误。

​$("#foo").on("mouseenter mouseleave", function(e){
    e.type === "mouseenter"
        ? $("#bar").appendTo(this)
        : $("#bar", this).remove() ;
});​

Fiddler: http://jsfiddle.net/AzEnm/1/ 提琴手: http : //jsfiddle.net/AzEnm/1/

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

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