How to make the Default focus in content page from master page

后端 未结 6 1850
我在风中等你
我在风中等你 2021-01-07 07:57

I have masterpage with content place holder. i have contentpage which is using master page . in all my content page i need to default focus on the text b

6条回答
  •  执念已碎
    2021-01-07 08:00

    If you use jQuery, a possible solution is:

    1. Give the textbox you want to set focus to a special class. "focus" works well for this purpose.

    2. Write code such as the following in your master page or included by your master page in a js script file:

      $(document).ready    
      (
      
        function()
        {
          //get an array of DOM elements matching the input.focus selector
          var focusElements = $("input.focus").get();
      
          //if a focus element exists
          if(focusElements.length > 0)
          {
            focusElements[0].focus();
          }
        }
      );
      

    A similar approach using vanilla JavaScript would be to tag the textbox with a special attribute. Let's use focus.

    window.onload = function()
    {
      //get all input elements
      var inputElements = document.getElementsByTagName("input");
    
      var elementToFocus = null;
    
      for(var i = 0; i < inputElements.length; ++i) 
      {
        var focusAttribute = inputElements[i].getAttribute("focus");
    
        if(focusAttribute)
        {
          elementToFocus = inputElements[i];
          break;
        }
      }
    
      if(elementToFocus)
      {
        elementToFocus.focus();
      }
    };
    

提交回复
热议问题