简体   繁体   English

jQuery选择器-#ElementId不起作用,仅ElementId可以

[英]JQuery selector - #ElementId not working just ElementId is fine

I am having some issues showing a div that i am hiding. 我在显示隐藏的div时遇到一些问题。 For some reason the #ElementId selector isnt working but if i just use ElementId it works. 由于某些原因,#ElementId选择器无法正常工作,但如果我只是使用ElementId,则可以正常工作。

Jquery 1.7.1 also I am using JQuery ui. jQuery 1.7.1我也正在使用jQuery UI。

$('CreateGroup').show("fold"); // this works fine
$('#CreateGroup').show("fold"); // this fails with cannot call show on undefined

 <div id="CreateGroup" style="display: none">Hi!</div>

What's going on? 这是怎么回事?

Thanks 谢谢

Are you sure the element is already created when you call the function? 您确定在调用函数时已经创建了元素吗?

try this to ensure it to run after the DOM is ready: 尝试执行以下操作以确保它在DOM准备就绪后运行:

$(window).ready(function() {
 $('#CreateGroup').show("fold");
});

More info: 更多信息:

.ready() 。准备()

Both should fail. 两者都应该失败。

Your code is not possible since you are not using the document.load event (aka $(function(){}); aka $('document').ready() . 由于您未使用document.load事件(aka $(function(){}); aka $('document').ready()因此无法执行代码。

The node have not yet been defined when the script is called. 调用脚本时尚未定义节点。

This would work however: 但是,这将起作用:

<div id="CreateGroup" style="display: none">Hi!</div>
<script type="text/javascript">$('#CreateGroup').show("fold");</script>

Since it's invoked AFTER the node definition. 由于它是在节点定义之后调用的。

You don't how to worry about that if you use the mentioned load methods. 如果使用上述加载方法,则无需担心。

<script type="text/javascript">
    $(function() {
        $('#CreateGroup').show("fold"); 
    });
</script>

<div id="CreateGroup" style="display: none">Hi!</div>

That will work since the script is not executed until the whole document have been loaded. 这将起作用,因为只有在加载整个文档后才执行脚本。

@Piercy, chances are your $() function comes from Prototype, not jQuery. @Piercy,您的$()函数可能来自Prototype,而不是jQuery。 Are you including both libraries in your page? 您是否将两个库都包含在页面中? – Frédéric Hamidi Mar 2 at 12:21 – 3月2日,弗雷德里克·哈米迪(FrédéricHamidi)12:21

This was correct it was prototype interfering. 这是对原型的干扰,这是正确的。 If I use the fully jQuery qualifier it works. 如果我使用完整的jQuery限定符,它将起作用。

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

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