如何在JavaScript中将addEventListener指定为ON?

时间:2017-07-05 15:34:03

标签: javascript dom javascript-events

如何将addEventListener指定为on 在纯JavaScript?不是jquery。



// i can do this
// assigning document as d
// assigning querySelector as $

const d = document
const $ = x => d.querySelector(x)

//but how to do this

$('button').addEventListener('click',doSomething)

//to

$('button').on('click',doSomething)

//assiging addEventListener as on 




2 个答案:

答案 0 :(得分:2)

您可以创建自己的const mapStateToProps = (state, props) => { return { mylist: state.mylist, }; }; const mapDispatchToProps = (dispatch, props) => { return { refreshStart: () => dispatch(recentRefreshStart()), mylistLoad: () => dispatch(mylistLoad()), }; }; export default connect(mapStateToProps, mapDispatchToProps)(MyList); 功能 -

on()

或者您可以查看对象函数的原型 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype

答案 1 :(得分:1)

不清楚你说的简单是什么意思,但这是另一种方式:

document.getElementById("id").onclick = doSomething;