English 中文(简体)
MooTools: Set focus on click
原标题:

I ve got an ASP.NET control that contains a checkbox. I want the focus to remain on this checkbox when it s clicked. Here s what I m using:

<script type="text/javascript">
    window.addEvent("domready", function() {

        var acceptCheckId = $("<%= this.accept.ClientID %>").getElement("input");

        acceptCheckId.addEvent("click", function() {
            acceptCheckId.focus();
        });

    });
</script>

I can get the checkbox element into the acceptCheckId variable okay, but when I click on it, the focus doesn t change. What gives?

If I add an alert in, the alert never fires:

    acceptCheckId.addEvent("click", function() {
        alert("foobar");
        acceptCheckId.focus();
    });
问题回答

What does your output HTML look like? it s impossible to say what s wrong w/ the code w/o knowing how your <% %> content is written.

<script type="text/javascript">
window.addEvent("domready", function() {

    var acceptCheckId = $("<%= this.accept.ClientID %>").getElement("input"); 
    // $( elementId ).getChildren( input );
    // You can t put an entire element in there                                                 

    acceptCheckId.addEvent("click", function() {
        acceptCheckId.focus();
    });

});
</script>

Try using setTimeout

setTimeout(function(){
  acceptCheckId.focus(); // this may execute ok 
},200);

When you click an element, the element will get focus, and you can t focus other element now, because this element will get focus first, and use setTimeout, may delay sometime, and after this time, you can set focus to any element.

look the click execution order: click --> execute onclick function --> get focus -->

In step 2, the onclick function, you set focus to other element, but, after execution the onclick function, the element itself should get focus, this done by web browser!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签