2016-12-01 95 views
2

e.target.href在firefox中不起作用。这只适用于铬。我尝试了以下问题的解决方案(event.target not working on Firefox),但仍然无法在Firefox上使用。我需要添加更多东西吗?e.target.href在firefox中不工作

app.js

showModal(e) { 
    if (!e) { 
     e = window.event; 
    } 
    e.preventDefault(); 
    const event = e.target || e.srcElement; 
    const url = event.href; 
    console.log('url', url, event.href); 
    const redirectTo = url.substring(url.lastIndexOf('/') + 1); 
    this.setState({ show: true }); 
    this.context.router.transitionTo(redirectTo); 
    } 

<Nav 
    showModal={(e) => this.showModal(e)} 
    hideModal={() => this.hideModal()} 
    show={this.state.show} 
    onHide={() => this.hideModal()} 
/> 

Nav.js

<button 
    className="btn btn-default" 
    onClick={(e) => props.showModal(e)} 
> 
<Link to={{ pathname: '/signup' }}> 
    {props.intl.formatMessage({ id: 'nav.registration.text' }) } 
</Link> 
</button> 
+0

什么是 “不工作在Firefox” 是什么意思?你得到了什么? – Oriol

回答

1

尝试使用getAttribute()

const tgt = e.target || e.srcElement; 
    const url = tgt.getAttribute('href'); 
    console.log('url', url, tgt); 

showModal(e) { 
 
    if (!e) { 
 
     e = window.event; 
 
    } 
 
    e.preventDefault(); 
 
    const tgt = e.target || e.srcElement; 
 
    const url = tgt.getAttribute('href'); 
 
    console.log('url', url, tgt); 
 
    const redirectTo = url.substring(url.lastIndexOf('/') + 1); 
 
    this.setState({ show: true }); 
 
    this.context.router.transitionTo(redirectTo); 
 
    }

+0

我也试过。我在Firefox中得到了null,但它在Chrome中工作,我也不需要操纵字符串。 – Serenity

+0

有不止一种方法去皮肤 - 试试:'var xHref = tgt.getAttributeNode(“href”).value' – zer00ne