Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取事件目标的子元素的索引_Javascript_Reactjs_Addeventlistener_Indexof - Fatal编程技术网

Javascript 获取事件目标的子元素的索引

Javascript 获取事件目标的子元素的索引,javascript,reactjs,addeventlistener,indexof,Javascript,Reactjs,Addeventlistener,Indexof,当我点击“photocontainer”类中的图像时,我得到了元素 如何获取该元素的索引 photoGallery(event) { console.log(event.target); this.setState({ imgSrc: event.target.src }); } <div className="photo-container" onClick={e=> this.photoGallery(e)} > <img src="https://im

当我点击“photocontainer”类中的图像时,我得到了元素

如何获取该元素的索引

photoGallery(event) {
  console.log(event.target);
  this.setState({ imgSrc: event.target.src });
}

<div className="photo-container" onClick={e=> this.photoGallery(e)} >
  <img src="https://img1.jpg" alt="img-1" />
  <img src="https://img2.jpg" alt="img-2" />
  <img src="https://img3.jpg" alt="img-3" />
</div>
photoGallery(事件){
console.log(event.target);
this.setState({imgSrc:event.target.src});
}
这张照片集(e)}>

最简单的方法是将click事件绑定到每个img并传入索引:

photoGallery(index, event) {
  console.log(event.target);
  this.setState({ imgSrc: event.target.src });
}

<div className="photo-container">
  <img src="https://img1.jpg" alt="img-1" onClick={this.photoGallery.bind(this, 0)} />
  <img src="https://img2.jpg" alt="img-2" onClick={this.photoGallery.bind(this, 1)} />
  <img src="https://img3.jpg" alt="img-3" onClick={this.photoGallery.bind(this, 2)} />
</div>
photoGallery(索引、事件){
console.log(event.target);
this.setState({imgSrc:event.target.src});
}

最简单的方法是将click事件绑定到每个img并传入索引:

photoGallery(index, event) {
  console.log(event.target);
  this.setState({ imgSrc: event.target.src });
}

<div className="photo-container">
  <img src="https://img1.jpg" alt="img-1" onClick={this.photoGallery.bind(this, 0)} />
  <img src="https://img2.jpg" alt="img-2" onClick={this.photoGallery.bind(this, 1)} />
  <img src="https://img3.jpg" alt="img-3" onClick={this.photoGallery.bind(this, 2)} />
</div>
photoGallery(索引、事件){
console.log(event.target);
this.setState({imgSrc:event.target.src});
}

为什么不将onClick处理程序附加到img元素而不是它的父元素

photoGallery(event) {
  console.log(event.target);
  this.setState({ imgSrc: event.target.src });
}

<div className="photo-container">
  <img src="https://img1.jpg" alt="img-1" onClick={e=> this.photoGallery(e)} />
  <img src="https://img2.jpg" alt="img-2" onClick={e=> this.photoGallery(e)} />
  <img src="https://img3.jpg" alt="img-3" onClick={e=> this.photoGallery(e)} />
</div>
photoGallery(事件){
console.log(event.target);
this.setState({imgSrc:event.target.src});
}
这个.photoGallery(e)}/>
这个.photoGallery(e)}/>
这个.photoGallery(e)}/>

为什么不将onClick处理程序附加到img元素而不是它的父元素

photoGallery(event) {
  console.log(event.target);
  this.setState({ imgSrc: event.target.src });
}

<div className="photo-container">
  <img src="https://img1.jpg" alt="img-1" onClick={e=> this.photoGallery(e)} />
  <img src="https://img2.jpg" alt="img-2" onClick={e=> this.photoGallery(e)} />
  <img src="https://img3.jpg" alt="img-3" onClick={e=> this.photoGallery(e)} />
</div>
photoGallery(事件){
console.log(event.target);
this.setState({imgSrc:event.target.src});
}
这个.photoGallery(e)}/>
这个.photoGallery(e)}/>
这个.photoGallery(e)}/>

创建
映像
组件并在映像上添加onClick事件处理程序:

const Image = ({path, alt, index, onClick}) =>
    <img
        src={path}
        alt={alt}
        onClick={e => onClick(e, index)}
    />;
const Image=({path,alt,index,onClick})=>
onClick(e,index)}
/>;
并在渲染函数中使用它:

const imagesList = [
    {
        path: 'https://img1.jpg',
        alt: 'img-1'
    },
    {
        path: 'https://img2.jpg',
        alt: 'img-2'
    },
    {
        path: 'https://img3.jpg',
        alt: 'img-3'
    }
];

const images = imagesList.map((image, i) => {
    return <Image
        {...image}
        onClick={this.photoGallery}
        key={i}
    />
});

return <div className="photo-container">
    {images}
</div>;
const imagesList=[
{
路径:'https://img1.jpg',
备选:“img-1”
},
{
路径:'https://img2.jpg',
备选:“img-2”
},
{
路径:'https://img3.jpg',
备选:“img-3”
}
];
const images=imagesList.map((图像,i)=>{
返回
});
返回
{图像}
;

创建
映像
组件并在映像上添加onClick事件处理程序:

const Image = ({path, alt, index, onClick}) =>
    <img
        src={path}
        alt={alt}
        onClick={e => onClick(e, index)}
    />;
const Image=({path,alt,index,onClick})=>
onClick(e,index)}
/>;
并在渲染函数中使用它:

const imagesList = [
    {
        path: 'https://img1.jpg',
        alt: 'img-1'
    },
    {
        path: 'https://img2.jpg',
        alt: 'img-2'
    },
    {
        path: 'https://img3.jpg',
        alt: 'img-3'
    }
];

const images = imagesList.map((image, i) => {
    return <Image
        {...image}
        onClick={this.photoGallery}
        key={i}
    />
});

return <div className="photo-container">
    {images}
</div>;
const imagesList=[
{
路径:'https://img1.jpg',
备选:“img-1”
},
{
路径:'https://img2.jpg',
备选:“img-2”
},
{
路径:'https://img3.jpg',
备选:“img-3”
}
];
const images=imagesList.map((图像,i)=>{
返回
});
返回
{图像}
;
以下是解决方案

解决方案A

单击容器并获取不可行的值,如果您有不同的图像名称,您将无法找到索引。但是,由于在上面给出的示例中有img-1、img-2等,因此有机会找到索引

photoGallery(事件、索引){
log(event.target.alt.replace('img-','');
this.setState({imgSrc:event.target.src});
} 
这张照片集(e)}>
以下是解决方案

解决方案A

单击容器并获取不可行的值,如果您有不同的图像名称,您将无法找到索引。但是,由于在上面给出的示例中有img-1、img-2等,因此有机会找到索引

photoGallery(事件、索引){
log(event.target.alt.replace('img-','');
this.setState({imgSrc:event.target.src});
} 
这张照片集(e)}>

如果要将事件侦听器保留在父对象上,可以从图像的alt属性中删除索引

 var alt = event.target.alt,
   index = alt.replace('img-', '');

或者,您可以使用@Igor Alemasow的建议,并使用
数据索引
HTML5属性来获取js中的索引

如果要将事件侦听器保留在父对象上,可以从图像的alt属性中剥离索引

 var alt = event.target.alt,
   index = alt.replace('img-', '');

或者,您可以使用@Igor Alemasow的建议,并使用
数据索引
HTML5属性来获取js中的索引

>你可以考虑将图像信息保持为一组图像对象。在组件中构建映像时,可以添加索引值并访问它。下面是一个使用您的代码的简化示例:

类PhotoGallery扩展了React.Component{
建造师(道具){
超级(道具);
this.state={imgSrc:''};
this.photoGallery=this.photoGallery.bind(this);
}
照片廊(活动){
log(event.target.dataset.index);
this.setState({imgSrc:event.target.src});
}
构建图像(图像){
返回images.map({index,src,alt})=>{
返回;
});
}
render(){
const{images}=this.props;
返回(
{this.buildImages(images)}
)
}
}
常量图像=[
{src:'https://img1.jpg,alt:'img-1',索引:1},
{src:'https://img2.jpg,alt:'img-2',索引:2},
//{src:'https://img3.jpg,alt:'img-3',索引:3},
//{src:'https://img4.jpg,alt:'img-4',索引:4},
{src:'https://img5.jpg,alt:'img-5',索引:5}
];
ReactDOM.render(
,
document.getElementById('容器')
);
img{
利润率:1米1米0;
边框:1px实心#dfdfdf;
填充:1em;
}


<>代码>你可以考虑将图像信息保持为一个图像对象数组。在组件中构建映像时,可以添加索引值并访问它。下面是一个使用您的代码的简化示例:

类PhotoGallery扩展了React.Component{
建造师(道具){
超级(道具);
this.state={imgSrc:''};
this.photoGallery=this.photoGallery.bind(this);
}
照片廊(活动){
log(event.target.dataset.index);
this.setState({imgSrc:event.target.src});
}
构建图像(图像){
返回images.map({index,src,alt})=>{
返回;
});
}
render(){
const{images}=this.props;
返回(
{this.buildImages(images)}
)
}
}
常量图像=[
{src:'https://img1.jpg'艾尔