function doSomethingWith(param)
{
document.body.addEventListener(
'scroll',
function()
{
document.write(param);
},
false
); // An event that I want to remove later
}
setTimeout(
function()
{
document.body.removeEventListener('scroll', HANDLER ,false);
// What HANDLER should I specify to remove the anonymous handler above?
},
3000
);
doSomethingWith('Test. ');
Run Code Online (Sandbox Code Playgroud) 以传统的方式添加事件监听器:
function getComboA(sel) {
var value = sel.options[sel.selectedIndex].value;
}
<select id="comboA" onchange="getComboA(this)">
<option value="">Select combo</option>
<option value="Value1">Text1</option>
<option value="Value2">Text2</option>
<option value="Value3">Text3</option>
</select>
Run Code Online (Sandbox Code Playgroud)
但我想适应addEventListener方式:
productLineSelect.addEventListener('change',getSelection(this),false);
function getSelection(sel){
var value = sel.options[sel.selectedIndex].value;
alert(value);
}
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为我不能将getSelection()中的任何参数作为addEventListener方法中的第二个参数传递?据我所知,我只能使用没有括号的函数名称.
任何的想法?
顺便说一下,请看看我之前关于console.log在safari 6.0开发人员检查器中不起作用的问题,我无法在控制台中编写任何输出,这令人沮丧.
我一直在玩es6类,并尝试设置一个简单的鼠标类.
addEventListener工作,但由于某种原因,我无法删除它们removeEventListener.我猜这与上下文绑定有关,但我无法弄清楚如何解决这个问题.
'use strict'
class Mouser {
constructor() {
this.counter = 0
this.clicked = function (event) {
this.counter++
console.log('clicks:', this.counter)
if (this.counter >= 10) this.remove()
}
window.addEventListener('click', this.clicked.bind(this))
}
remove() {
console.log('Removing click listener') // this line runs ..
window.removeEventListener('click', this.clicked.bind(this))
}
}
var mouse = new Mouser()
Run Code Online (Sandbox Code Playgroud) 可能重复:
删除使用bind添加的事件侦听器
我需要动态添加和删除事件侦听器.我还需要设置this.
这会改变功能参考吗?
element.addEventListener('click', funcA);
newFunc = funcA.bind(this);
element.removeEventListner('click', newFunc);
Run Code Online (Sandbox Code Playgroud)
removeEventListener会知道我要删除funcA吗?
或者它认为我一起删除一个新功能?
如何删除我window在 constructor 下面绑定的点击侦听器?我需要它来监听window,我需要访问它里面的按钮实例。
class MyEl extends HTMLButtonElement {
constructor() {
super();
this.clickCount = 0;
window.addEventListener('click', this.clickHandler.bind(this));
}
clickHandler(e) {
if (e.target === this) {
this.textContent = `clicked ${++this.clickCount} times`;
window.removeEventListener('click', this.clickHandler);
}
}
disconnectedCallback() {
window.removeEventListener('click', this.clickHandler);
}
}
customElements.define('my-el', MyEl, { extends: 'button' });Run Code Online (Sandbox Code Playgroud)
<button is="my-el" type="button">Click me</button>Run Code Online (Sandbox Code Playgroud)
javascript addeventlistener ecmascript-6 custom-element removeeventlistener
我设法通过孩子传递上下文,但只有一次。上下文永远不会更新。然而我已经看到很多这样的例子,包括反应文档:https : //facebook.github.io/react/docs/context.html
这是我的代码:
父组件:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
window:{
height:null,
width:null
}
};
}
getChildContext() {
return {
window: this.state.window
}
}
componentDidMount () {
window.addEventListener('resize', this.handleResize.bind(this));
this.handleResize();
}
componentWillUnmount () {
window.removeEventListener('resize', this.handleResize.bind(this));
}
handleResize (){
this.setState({
window:{
width:window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth,
height:window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight
}
});
}
render() {
console.log(this.state.window);
// --> working
return (
{this.props.children}
);
}
}
App.propTypes = {
children: React.PropTypes.node.isRequired
};
App.childContextTypes …Run Code Online (Sandbox Code Playgroud) 内部对象构造函数:
this.notification.addEventListener(barcodeScanner.NEW_READING, this.handleBarcode.bind(this));
Run Code Online (Sandbox Code Playgroud)
当它失败时:
this.notification.removeEventListener(barcodeScanner.NEW_READING, this.handleBarcode.bind(this), this);
Run Code Online (Sandbox Code Playgroud)
我可以添加事件监听器并正常工作,但是当对象销毁时我无法删除单个事件监听器.
虽然它与问题没有多大关系,但我使用的是EventDispatcher.js和Class.js.
我可以修改EventDispatcher.js中的代码以满足我的需要.但是如何在不删除所有其他侦听器的情况下删除objcet函数的事件侦听器?