最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Need to put focus on div in react - Stack Overflow

programmeradmin6浏览0评论

I have a text which was earlier wrapped in an H1 tag. I need to focus on that text once my page is loaded. I wrapped it in a div for my convenience.

render() {
    const { translate, billing: { primaryContactSelection = true } } = this.props;
    return (
      <div {...resolve(BillingStyles, 'billingContainer')}>
        <div id="mainHeader"><h1 {...resolve(BillingStyles, 'mainHeader')}>
          {translate('PanelBillingHeadingText')}
        </h1> </div>
        <div {...resolve(BillingStyles, 'billingInfoContainer')}>
         ......
         ......
        </div>
      </div>
    );
  }
}

I have tried the below code:

ponentDidMount() {
    console.log('Component Did Mount .............');
    document.getElementById('#mainHeader').focus();
  }

But it is not focusing on the div.

I have a text which was earlier wrapped in an H1 tag. I need to focus on that text once my page is loaded. I wrapped it in a div for my convenience.

render() {
    const { translate, billing: { primaryContactSelection = true } } = this.props;
    return (
      <div {...resolve(BillingStyles, 'billingContainer')}>
        <div id="mainHeader"><h1 {...resolve(BillingStyles, 'mainHeader')}>
          {translate('PanelBillingHeadingText')}
        </h1> </div>
        <div {...resolve(BillingStyles, 'billingInfoContainer')}>
         ......
         ......
        </div>
      </div>
    );
  }
}

I have tried the below code:

ponentDidMount() {
    console.log('Component Did Mount .............');
    document.getElementById('#mainHeader').focus();
  }

But it is not focusing on the div.

Share Improve this question edited Dec 20, 2018 at 21:34 BSMP 4,8078 gold badges35 silver badges45 bronze badges asked Dec 20, 2018 at 11:46 Ishant GauravIshant Gaurav 1,2034 gold badges15 silver badges34 bronze badges 5
  • Possible duplicate of Focusing div elements with React – Sameer Commented Dec 20, 2018 at 11:50
  • Possible duplicate of Is it possible to focus on a <div> using JavaScript focus() function? – Monarth Sarvaiya Commented Dec 20, 2018 at 11:50
  • I have tried these , but it is not working fro me – Ishant Gaurav Commented Dec 20, 2018 at 11:59
  • Can you explain what you mean by focus on div? divs are not focusable – Chris Commented Dec 20, 2018 at 12:00
  • I have a text which was earlier wrapped in h1 tag , i need to focus on that text once my page is loaded . I wrapped in div for my convinience. How can i do that ? – Ishant Gaurav Commented Dec 20, 2018 at 12:02
Add a ment  | 

2 Answers 2

Reset to default 13

First div elements are not focusable by default so you need to give it a tabIndex:

render() {
    const { translate, billing: { primaryContactSelection = true } } = this.props;
    return (
      <div {...resolve(BillingStyles, 'billingContainer')}>
        <div tabIndex="0" id="mainHeader"><h1 {...resolve(BillingStyles, 'mainHeader')}>
          {translate('PanelBillingHeadingText')}
        </h1> </div>
        <div {...resolve(BillingStyles, 'billingInfoContainer')}>
         ......
         ......
        </div>
      </div>
    );
  }
}

Next make sure you don't include the hashtag when calling getElementById, so it should be :

ponentDidMount() {
    console.log('Component Did Mount .............');
    document.getElementById('mainHeader').focus();
  }

And that should work from there.

When you use getElementById function you don't need # sign inside braces.

发布评论

评论列表(0)

  1. 暂无评论