简体   繁体   中英

Function Event Target in Javascript

I'm trying to get "event.target" to work and cannot. Check out the fiddle below. It works in Safari and Chrome, but not Firefox. I get nothing there.

mySpan.addEventListener('mouseup', function(){ textClick(); }, false)

function textClick (){
    outPutBox.innerHTML = event.target.textContent
}

Here's the full fiddle: https://jsfiddle.net/dmperkins74/ochvvn0o/

In the FF console, it says "event is not defined" but why is it working in the other browsers?

I'm quite a rookie, so please be gentle. Any help appreciated. Also, please don't throw any JQuery my way... I'm just not "there" yet.

Thanks, Dan P.

Because event is not defined. It is passed from the event listener.

mySpan.addEventListener('mouseup', textClick, false)

function textClick (event) {
    outPutBox.innerHTML = event.target.textContent
}

or

mySpan.addEventListener('mouseup', function(e){ textClick(e); }, false)

function textClick (event) {
    outPutBox.innerHTML = event.target.textContent
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM