如何用 jquery 隐藏这个元素?
这是在浏览器中呈现的 html 代码。我想隐藏 div.embed
元素:
<div id='video_div'>
<img src="http://i3.ytimg.com/vi/fTWpHknumdg/hqdefault.jpg" style="width: 200px; ">
<div class="embed">
<object width="300" height="194">
<param name="wmode" value="opaque"><param name="movie" value="http://www.youtube.com/v/fTWpHknumdg?version=3">
<param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always">
<embed src="http://www.youtube.com/v/fTWpHknumdg?version=3" type="application/x-shockwave-flash" width="300" height="194" allowscriptaccess="always" allowfullscreen="true" wmode="opaque">
</object>
</div>
</div>
这是我现在运行的 js 代码。这对我不起作用。然而,它似乎在第一个答案提供的 jsfiddle 中工作:
$(document).ready(function() {
$('a.oembed').embedly({maxWidth:300,'method':'replace'}).bind('embedly-oembed', function(e, oembed){
$("#video_div").prepend($("<img>", { src: oembed.thumbnail_url, width:200 }));
});
$('div.embed').hide();
});
是否在加载元素后调用隐藏代码?我该如何解决这个问题?
Here's the html code rendered in the browser. I want to hide the div.embed
element:
<div id='video_div'>
<img src="http://i3.ytimg.com/vi/fTWpHknumdg/hqdefault.jpg" style="width: 200px; ">
<div class="embed">
<object width="300" height="194">
<param name="wmode" value="opaque"><param name="movie" value="http://www.youtube.com/v/fTWpHknumdg?version=3">
<param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always">
<embed src="http://www.youtube.com/v/fTWpHknumdg?version=3" type="application/x-shockwave-flash" width="300" height="194" allowscriptaccess="always" allowfullscreen="true" wmode="opaque">
</object>
</div>
</div>
Here's the js code I'm running now. This doesn't work for me. However it seems to work in the jsfiddle provided by the first answer:
$(document).ready(function() {
$('a.oembed').embedly({maxWidth:300,'method':'replace'}).bind('embedly-oembed', function(e, oembed){
$("#video_div").prepend($("<img>", { src: oembed.thumbnail_url, width:200 }));
});
$('div.embed').hide();
});
Could it be that the hide code is being called after the element is loaded? How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是否需要使用Javascript来隐藏DIV? 做到这一点要简单得多
使用 CSS Update
CSS 将处理初始状态;此后 Javascript 代码将启用隐藏/显示 DIV。
Is it necessary to use Javascript to hide the DIV? Much simpler to do it with CSS
Update
The CSS will take care of the initial state; the Javascript code will enable hiding/showing the DIV's thereafter.
我尝试过(jsfiddle):
这工作正常。也许隐藏代码在元素加载到 DOM 之前被调用?
I tried (jsfiddle):
And this works fine. Perhaps the hide code is being called before element is loaded into the DOM?
尝试将它们链接在一起。所以像这样(使用children()来查找内部div:
如果它总是被认为是隐藏的,那么只需通过css来实现。
更新:
尝试使用.load() in
$(document).ready()
:一旦元素和所有内容都将触发其中已加载。
Try Chaining them together. So something like this (use children() to find the inner div:
If it is always suppose to be hidden then just do it via css.
Update:
Try using .load() in
$(document).ready()
:This will fire once the element and everything within it is loaded.