In WEB projects, we often encounter dynamically adding events to the corresponding object, as follows, with text box control with id="txtPrice":
<div> <input type="text" id="txtPrice" name = "txtPrice" value = "0"/> <div>
Now we add an event dynamically to it, and the JS core code is as follows:
document.getElementById("txtPrice").attachEvent('onblur', function (){alert('Add event successful!')});【Replenish】
Sometimes in order to achieve previous compatibility between different browsers, we would write this:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1 if(window.attachEvent){ document.getElementById("txtPrice").attachEvent('onblur', function (){alert('Add event successful!')}); }else{ document.getElementById("txtPrice").addEventListener('onblur', function (){alert('Add event successful!')}, false);}The simple way to add events to objects in the above JS dynamics is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.