简体   繁体   English

将jQuery与隐藏div中的元素一起使用?

[英]Using jQuery with elements in hidden div?

Don't know if this is posible.. 不知道这是否可行。

I have an ASP.Net page that has a hidden div on it. 我有一个ASP.Net页面,上面有一个隐藏的div。 When the user performs some certain function the div becomes visible ( display='block' ). 当用户执行某些特定功能时,div变为可见( display='block' )。 This is happening in the VB.Net code behind for many reasons. 发生在VB.Net代码背后的原因很多。

dvDetail.Style("display") = "block"

I need to have some jQuery assign a plugin to a TextBox that is in the hidden div but I am having a problem doing that - I am a bit new to jQuery. 我需要让一些jQuery将一个插件分配给位于隐藏div中的TextBox,但是这样做有问题-我对jQuery有点陌生。 It seems that the TextBox does not exist on the page after it is loaded because of the display='none' of the div at that time and by the time the user performs the action that causes the div to display it is too late for the $(document).ready because it has already run. 似乎TextBox在加载后在页面上不存在,因为当时div的display='none' ,并且在用户执行导致div显示的动作时,对于$(document).ready因为它已经运行。 I have tested the functionality in a non-hidden part of the page and the jQuery works just fine. 我已经在页面的非隐藏部分中测试了功能,而jQuery可以正常工作。

To respond to many of your fine answers - the foloowing test code is as close as I got to what I actually need - it actually works but it causes the hidden div to become visable when the page first loads which is not what I need. 为了回答您的许多好答案-逐渐下降的测试代码与我实际需要的内容非常接近-它实际上有效,但是当页面首次加载时,它会使隐藏的div变得可见,这不是我所需要的。

$(document).ready(function () {
    $('[id$=dvDetail]').show(function () {
        $('[id$=btnDelete]').click(function () {
            alert('Delete clicked');
        });
    });
});

I don't have experience with asp.net, but if the element (div) is loaded in the DOM, jQuery should be able to find it, wether it's hidden or not. 我没有使用asp.net的经验,但是如果元素(div)加载到DOM中,则jQuery应该能够找到它,无论它是否隐藏。 Have you checked if the element does indeed load in the DOM. 您是否检查过元素是否确实在DOM中加载。 You could check that (and debug the javascript) with Chrome's developer tools, or FF's firebug. 您可以使用Chrome的开发人员工具或FF的firebug进行检查(并调试javascript)。

尝试将您的jquery代码放在$(document).ready函数之外,但请确保它存在于文本框html之后。

There's a difference between the ASP.NET control property .Visible = False and the CSS attribute display: none - if the control is set to .Visible = False , the control is never rendered on the client side, and as a result any client-side scripts will not be able to find it. ASP.NET控件属性.Visible = False和CSS属性display: none之间有区别display: none如果控件设置为.Visible = False ,则控件永远不会在客户端呈现,因此任何客户端-旁脚本将无法找到它。

Check as jcane86 said to make sure the control is actually rendered on the client side - it may not be displaying, but it should be in the actual HTML output of the page. 按照jcane86的说明进行检查,以确保控件实际上是在客户端呈现的-它可能不会显示,但应该在页面的实际HTML输出中。 If it's there, jQuery should be able to find it (and if you debug Javascript w/ your browser tools of choice, you can test with $("#MyClientSideId").show() in your JavaScript console, as Hunter said). 如果有的话,jQuery应该能够找到它(并且,如果您使用所选的浏览器工具调试Javascript,则可以在JavaScript控制台中使用$("#MyClientSideId").show()进行测试,就像Hunter所说的那样)。

You can get all the hidden elements like this: 您可以像这样获得所有隐藏的元素:

$("div:hidden").each(function(){ alert($(this).attr("id")); });

Hope that helps 希望能有所帮助

Consider the following html: 考虑以下html:

<div style="" id="hide">
  <textarea ></textarea>
</div>

I'm able to manipulate the hidden textarea with jquery then show it at the end of the chaining. 我可以使用jquery处理隐藏的textarea,然后在链接结束时显示它。

 $("#hide").children('textarea')
.text('hello there')
.css({width: '300px', height: '300px', padding: '5px', border: '1px solid #ccc'})
.parent().show();

Hope it helps. 希望能帮助到你。 Cheers 干杯

PS: If the element is being loaded at a later time after the document ready has finished for example, you will need to bind an event for the code above to work, using the .bind() method or my favorite .delegate() PS:例如,如果在准备好文档之后稍后再加载元素,则需要使用.bind()方法或我最喜欢的.delegate()绑定一个事件,以使上述代码起作用。

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

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