jQuery create element fires onclick event
问题 I create a anchor using jQuery and the onclick event seems be triggered when the element is created. I've used this method of creating elements a few times with this project without a problem, have I got the wrong end of the stick? jQuery('<a/>', { href: '#', name: 'link_html_edit', id: 'link_html_edit', html: 'input HTML text', onclick: alert('test') }).appendTo(spanDefaultValue); Thanks 回答1: You're calling alert('test'); and assigning it's return value to onclick . Use this instead: onclick