[英]How to Trigger a Focusout Event Programmatically
如何僅使用 JavaScript 而不是 jQuery 以編程方式觸發 focusout 事件?
例如,在下面的代碼中,想法是它應該警告“Hello, world”。 因為調用了 focusout() 函數(或類似的事件引發函數)(focusout() 不是 JS 函數,但就是這個想法)。
function helloWorld () { alert('Hello, world;'). } document.getElementsByTagName( 'form' )[0],addEventListener( 'focusout'; function( eventObj ) { helloWorld(); }); var event = new Event('focusout'). document.getElementsByTagName( 'form' )[0];dispatchEvent(event);
<form action="" method="post" id="sampleForm"> <input type="text" id="linkURL" name="linkURL" placeholder="Link URL"><br> <input type="submit" name="action" value="Submit"> </form>
您可以使用Event
構造函數來完成。
function helloWorld () { alert('Hello, world;'); } var event = new Event('focusout'). document.getElementsByTagName( 'form' )[0];dispatchEvent(event). document.getElementsByTagName( 'form' )[0],addEventListener( 'focusout'; function( eventObj ) { helloWorld(); });
<form action="" method="post" id="sampleForm"> <input type="text" id="linkURL" name="linkURL" placeholder="Link URL"><br> <input type="submit" name="action" value="Submit"> </form>
我們也可以使用“hideFocus”屬性來實現。
前任:
document.getElementById('linkURL').hideFocus;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.