如何移除预先准备好的容器
How to remove a container prepended to the body
我已经创建了一个容器,并将其准备到正文中:
// Create container
var container = "<div class='foo'> ... </div>";
// Prepend it to the body
$("body").prepend(container);
现在我想在几秒钟后淡出容器:
setTimeout(function() {
container.fadeOut();
}, 3000);
然而,它说undefined不是一个函数。我很确定$(".foo").fadeOut()
会起作用,但我有很多.foo
容器,我不想为每个容器分配单独的id。
使container
var指向jq对象而不是字符串:
var container = $('<div class="foo"> ... </div>');
// Prepend it to the body
$("body").prepend(container);
setTimeout(function() {
container.fadeOut();
}, 3000);
现在,container
有一个fadeOut()
方法,而不是一个哑字符串。
编辑:根据请求,创建部分的香草版本:
var container = document.createElement("div");
container.className="foo";
container.innerHTML= "<b>Hello world</b>";
document.body.insertBefore(container, document.body.firstChild);
香草中的fadeOut部分:
<style>
div.foo{ opacity: 1; transition: 1000ms opacity;}
div.foo.fade { opacity: 0; }
</style>
setTimeout(function(){ container.classList.add("fade");}, 3000);
应该是这样的:
var container = $("<div class=`foo`><h1>Hello World</h1></div>")
$("body").prepend(container);
setTimeout(function() {
container.fadeOut();
}, 3000);
此处的工作代码
这样尝试:
$(document).ready(function(){
var container = $("<div class='foo'><h1>Something...</h1></div>")
$("body").prepend(container);
setTimeout(function() {
container.fadeOut();
}, 3000);
});
您正在将一个字符串传递给您的'container'变量。fadeOut功能仅适用于DOM对象。不是字符串。使用将字符串转换为DOM对象
var container = $('Your Code Here');
container
是字符串变量。容器字符串表示的DOM元素可以像$('div.foo')
一样访问。所以你的fadeout
呼叫会像->一样改变
setTimeout(function() {
$('div.foo').fadeOut();
}, 3000);
相关文章:
- 在jquery文档准备好之前加载Modernizr
- 请求准备一个文件并返回路径,允许用户在准备好时下载
- Electron:Express.js服务器在Mongo数据库'It’准备好了
- 从Document调用onchange函数.已准备好从更新中预先选择的项目
- jquery触发器可以从点击开始工作,但没有准备好
- 我怎么知道Jqgrid已经准备好了
- Javascript+HTML中的日期选择器准备好了吗?(需要:DD、MM、YY的单独字段加上日历选择器)
- 创建元素更好还是只在html上准备好它们
- 如何移除预先准备好的容器
- 加载直到图像准备好,而图像隐藏直到它准备好
- 将参数传递到准备好文档的.js文件
- 如何一次插入两个表?准备好的发言
- 杜兰达尔.js文件准备好了
- $().bind 在未准备好文档的情况下无法正常工作
- 文档准备好与 UI Dilaog
- 需要在设备中准备好文档
- JW播放器寻找准备好
- 加载应用程序.js在设备中准备好煎茶触摸科尔多瓦应用程序
- jQuery文档是否准备好等待通过Backbone动态注入的html元素
- 检查对象何时准备好使用 javascript