How to focus on element that loads after page load

…衆ロ難τιáo~ 提交于 2019-12-31 05:31:06

问题


I am using the following hack-ish approach to focus on the Stripe input element after it's loaded:

<div id="card-element">
  <!-- Load stripe stuff here -->
</div>

// <script src="https://js.stripe.com/v3/"></script>
var card = elements.create('card', {style: style});
card.mount('#card-element');

setTimeout(function(){
    card.focus();
},1000);

Is there a better way to do, i.e., to detect when the card element is 'focus-able'. If I do it on page-load, it just doesn't see the element (it probably hasn't been added yet, and so it doesn't work).

Here's an example element: https://stripe.github.io/elements-examples/ (I wasn't able to paste a working example into jsfiddle, as I wasn't sure how to load an external library).


回答1:


As per the docs you can attach a listener on the element and then call focus when it has mounted.

Not tested code but should look something like this

 card.on('ready', function(){
    card.focus();
    }
)


来源:https://stackoverflow.com/questions/52883195/how-to-focus-on-element-that-loads-after-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!