javascript 使用 jQuery 隐藏元素不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20615525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hiding an element with jQuery doesn't work
提问by kpagcha
I am trying to create a script to develop a chained select, but the simplest thing doesn't work. Note that a know very little about js and jquery.
我正在尝试创建一个脚本来开发一个链式选择,但最简单的事情不起作用。请注意,对 js 和 jquery 知之甚少。
I create my dropdown list with CodeIgniter: <?php echo form_dropdown('city', array(), "", 'id="ciudades"'); ?>
我使用 CodeIgniter 创建我的下拉列表: <?php echo form_dropdown('city', array(), "", 'id="ciudades"'); ?>
Then load the script:
然后加载脚本:
if (isset($add_select_sources))
{
echo "
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script>
<script src='" . $root_path . "js/jquery-1.10.2.js'></script>
<script type='text/javascript' src='" . $root_path . "js/select.js'></script>
";
}
I am sure that add_select_sources is true, it is tested.
我确信 add_select_sources 是真的,它已经过测试。
And then my select.js: $('#ciudades').hide();
然后是我的 select.js: $('#ciudades').hide();
What am I doing wrong?
我究竟做错了什么?
回答by DarkMantis
Make sure that your javascript is being executed after jQuery is being included.
确保在包含 jQuery 后正在执行您的 javascript。
Also, you should execute your jQuery within a "Document Ready closure":
此外,您应该在“文档就绪闭包”中执行您的 jQuery:
$(document).ready(function(){
$('#ciudades').hide();
});
There have been issues with people trying to do this. So there are some more things I'd like to suggest:
试图这样做的人存在问题。所以我还有一些建议:
- Make sure that your elements have a width/height/display-block etc
- Try $('#ciudades').show().hide(); as some people have had that issue too!
- console.log() your events to see if they are being fired:
- 确保您的元素具有宽度/高度/显示块等
- 试试 $('#ciudades').show().hide(); 因为有些人也有这个问题!
- console.log() 您的事件以查看它们是否被触发:
$(document).ready(function(){ console.log('doc ready'); $('#ciudades').show().hide(); console.log('element hidden'); });
$(document).ready(function(){ console.log('doc ready'); $('#ciudades').show().hide(); console.log('element hidden'); });
Also, why are you using PHP to echo out script tags?
另外,你为什么使用 PHP 来回显脚本标签?
回答by dev1234
This will easily do.
这很容易做到。
$(document).ready(function(){
$('#yourEelementId').hide();
});