Javascript 如何卸下车身上预先安装的容器

Javascript 如何卸下车身上预先安装的容器,javascript,jquery,html,Javascript,Jquery,Html,我已经创建了一个容器并将其添加到正文中: // Create container var container = "<div class='foo'> ... </div>"; // Prepend it to the body $("body").prepend(container); 然而,它说未定义不是一个函数。我很确定$(“.foo”).fadeOut()会起作用,但我有很多.foo容器,我不想为每个容器分配单独的id 使您的容器变量指向jq对象而不是字符串:

我已经创建了一个容器并将其添加到正文中:

// Create container
var container = "<div class='foo'> ... </div>";

// Prepend it to the body
$("body").prepend(container);

然而,它说未定义不是一个函数。我很确定
$(“.foo”).fadeOut()
会起作用,但我有很多
.foo
容器,我不想为每个容器分配单独的id

使您的
容器
变量指向jq对象而不是字符串:

var container = $('<div class="foo"> ... </div>');

// Prepend it to the body
$("body").prepend(container);

setTimeout(function() {
   container.fadeOut();
}, 3000);
香草的淡出部分:

<style> 
        div.foo{ opacity: 1; transition: 1000ms opacity;}
        div.foo.fade { opacity: 0; }
</style>

  setTimeout(function(){ container.classList.add("fade");}, 3000);

div.foo{不透明度:1;转换:1000ms不透明度;}
div.foo.fade{opacity:0;}
setTimeout(函数(){container.classList.add(“fade”);},3000);

容器
是字符串变量。容器字符串指示的DOM元素可以像
$('div.foo')
一样访问。因此,您的
fadeout
呼叫将更改为->

setTimeout(function() {
    $('div.foo').fadeOut();
}, 3000);
应该是这样的:

var container = $("<div class=`foo`><h1>Hello World</h1></div>")
$("body").prepend(container);
setTimeout(function() {
    container.fadeOut();
}, 3000);
var container=$(“你好世界”)
$(“正文”)。前置(容器);
setTimeout(函数(){
容器。淡出();
}, 3000);
工作代码

尝试如下:

$(document).ready(function(){
   var container = $("<div class='foo'><h1>Something...</h1></div>")
   $("body").prepend(container);
   setTimeout(function() {
   container.fadeOut();
   }, 3000);
});
$(文档).ready(函数(){
var container=$(“某物…”)
$(“正文”)。前置(容器);
setTimeout(函数(){
容器。淡出();
}, 3000);
});

您正在向“container”变量传递字符串。淡出功能仅适用于DOM对象。不是字符串。使用以下命令将字符串转换为DOM对象:

var container = $('Your Code Here');

这个确实有效。你能告诉我多少美元吗;看起来像纯JS(以防万一)?我预计会出现一个错误
container.fadeOut不是一个函数。
想知道为什么当
container
是一个
字符串时他会得到
未定义的
,这不是一个坏点,但我假设错误报告是非逐字的,只是不起作用…@sabithpocker,我的坏。它说undefined不是一个函数。@dandavis,感谢纯JS版本。这对我来说更有意义。
$(document).ready(function(){
   var container = $("<div class='foo'><h1>Something...</h1></div>")
   $("body").prepend(container);
   setTimeout(function() {
   container.fadeOut();
   }, 3000);
});
var container = $('Your Code Here');