javascript - React onClick event on link - Stack Overflow

I have this weird thing i have this simple ponentimport React, {Component} from 'react';cons

I have this weird thing i have this simple ponent

import React, {Component} from 'react';
const Logo = ({onClick}) => {
return (

    <a name="home" onClick={onClick} className="logo">

        <span className="logo-mini"></span>



    </a>

);
};


export default Logo;

the onClick event suppose to to get the click event on the link to get attribute name but what i got is undefined when i console.log the event.target the out put is <span class="logo-lg"></span>

in the root penent my render methode call <Logo onClick={this.handleClick}/> handleClick method

    handleClick(){
    let to = event.target.name;
    console.log(event.target);
    }

I have this weird thing i have this simple ponent

import React, {Component} from 'react';
const Logo = ({onClick}) => {
return (

    <a name="home" onClick={onClick} className="logo">

        <span className="logo-mini"></span>



    </a>

);
};


export default Logo;

the onClick event suppose to to get the click event on the link to get attribute name but what i got is undefined when i console.log the event.target the out put is <span class="logo-lg"></span>

in the root penent my render methode call <Logo onClick={this.handleClick}/> handleClick method

    handleClick(){
    let to = event.target.name;
    console.log(event.target);
    }
Share Improve this question asked Sep 18, 2016 at 15:16 AmineAmine 1722 gold badges3 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can access the desired attribute by chaining parentElement with the getAttribute() method. Try this in you handleClick method

console.log(event.target.parentElement.getAttribute('name'));

More info here:
https://developer.mozilla/en-US/docs/Web/API/Element/getAttribute https://developer.mozilla/en/docs/Web/API/Node/parentElement


Alternatively, you could use:
console.log(event.target.getAttribute('name')); but this would require that you put the name attribute on your span element.

Also, in case the gist gets deleted, here is a full working code:

class Header extends React.Component { 
  handleClick(event) {
    console.log(event.target.parentElement.getAttribute('name'));
  }
  render() {
    return (
      <Logo onClick={this.handleClick}/>
    );
  }
}

const Logo = ({onClick}) => {
  return (
    <a name="home" onClick={onClick} className="logo">
        <span className="logo-mini">Logo</span>
    </a>
  );
};

ReactDOM.render(<Header/>, document.getElementById('app'));

Link to codepen: http://codepen.io/PiotrBerebecki/pen/LRRYRq

You need to bind this to your function call.

 <Logo onClick={this.handleClick.bind(this)}/>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744121027a4559400.html

相关推荐

  • javascript - React onClick event on link - Stack Overflow

    I have this weird thing i have this simple ponentimport React, {Component} from 'react';cons

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信