--- < form> < textarea onkeypress =" foo(this,event)">< / textarea> < input type =" button"值= QUOT;栏()" onclick =" bar()"> < / form> < script type =" text / javascript"> 函数foo(elem,evt){ alert( [ " elem:" + elem .nodeName, " shiftKey:" + evt.shiftKey, " keyCode:" + evt.keyCode ]。加入(" \ n") ); } 功能栏(){ if(document.createEventObject){ var evt = document.createEventObject(); evt.shiftKey = true; evt .keyCode = 121; document.forms [0] .elements [0] .fireEvent(" onkeypress",evt); } } < / script> --- HTH, 是的。 You seem to be targeting IE only, so the solution will be IE only. Notethat triggering a keypress event does not deal with the built-inpre-event mechanics, i.e. you won''t see any letter in your textarea. Asfor manipulating the textarea''s value, use IE ranges. Though the idea of triggering events is a good one, I believe that thereusually are better approaches, using handlers and "proxies", but notknowing what you want to do makes it difficult to determine the best one:-)---<form><textarea onkeypress="foo(this, event)"></textarea><input type="button" value="bar()" onclick="bar()"></form> <script type="text/javascript">function foo(elem, evt){alert(["elem : " + elem.nodeName,"shiftKey : " + evt.shiftKey,"keyCode : " + evt.keyCode].join("\n"));} function bar(){if(document.createEventObject) {var evt=document.createEventObject();evt.shiftKey=true;evt.keyCode=121;document.forms[0].elements[0].fireEvent("onkeypress", evt);}}</script>---HTH,Yep. 您似乎只针对IE,因此该解决方案仅限IE。注意触发按键事件不会处理内置的事件前机制,即你在textarea中看不到任何字母。为了操纵textarea的值,使用IE范围。 虽然触发事件的想法很好,但我相信那里通常是更好的方法,使用处理程序和代理,但不知道你想做什么使得很难确定最好的一个: - ) You seem to be targeting IE only, so the solution will be IE only. Note that triggering a keypress event does not deal with the built-in pre-event mechanics, i.e. you won''t see any letter in your textarea. As for manipulating the textarea''s value, use IE ranges. Though the idea of triggering events is a good one, I believe that there usually are better approaches, using handlers and "proxies", but not knowing what you want to do makes it difficult to determine the best one:-) 我想我也不知道自己:)但我不认为这是我的问题的解决方案 。当我在阅读关于这个主题的更多内容时,我也注意到了 这里的方法名为''handleEvent()''。 fireEvent和handleEvent有什么区别? - ,_, (O,O) () ---" - " -alex I think I don''t know myself either :) But I don''t think this is the solutionto my problem. While I was reading some more on this subject I also noticedthere''s method called ''handleEvent()''. What''s the difference betweenfireEvent and handleEvent? --,_,(O,O)( )---"-"-alex 这篇关于fireEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 17:31