相关疑难解决方法(0)

如何使用匿名函数removeEventListener作为addEventListener?

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)

javascript anonymous-function event-handling

85
推荐指数
3
解决办法
9万
查看次数

如何在addEventListener中使用参数传递给函数?

以传统的方式添加事件监听器:

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开发人员检查器中不起作用的问题,我无法在控制台中编写任何输出,这令人沮丧.

html javascript javascript-events

53
推荐指数
3
解决办法
8万
查看次数

javascript removeEventListener不在类中工作

我一直在玩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)

javascript class javascript-events

21
推荐指数
1
解决办法
5474
查看次数

bind()是否会更改函数引用?| 如何永久设置?

可能重复:
删除使用bind添加的事件侦听器

我需要动态添加和删除事件侦听器.我还需要设置this.

这会改变功能参考吗?

element.addEventListener('click', funcA);
newFunc = funcA.bind(this);
element.removeEventListner('click', newFunc);
Run Code Online (Sandbox Code Playgroud)

removeEventListener会知道我要删除funcA吗?

或者它认为我一起删除一个新功能?

javascript

7
推荐指数
1
解决办法
3550
查看次数

移除使用 bind(this) 添加的事件监听器

如何删除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

6
推荐指数
2
解决办法
2353
查看次数

React 上下文已通过但在 setState 后未更新

我设法通过孩子传递上下文,但只有一次。上下文永远不会更新。然而我已经看到很多这样的例子,包括反应文档: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)

reactjs react-router

5
推荐指数
0
解决办法
1281
查看次数

如何使用.bind(this)删除对象的事件侦听器?

内部对象构造函数:

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函数的事件侦听器?

javascript event-handling javascript-events

3
推荐指数
1
解决办法
3668
查看次数