0

我有这段代码,它是一个标签,onClick里面有一个事件。我的问题是,当我在浏览器中单击 div 时,会触发 href,然后我将转到另一个站点。有没有办法确保只有 onClick is triggered when clicking onchnageOrder onClick` 和链接在单击 a 标签的其余部分时有效?

  <div key={idx.toString()}>
    <a className="stage-container" href={stageUrl}>
      <h3>{stage.title}</h3>
      <div className="arrow-container">
        <div
          className=""
          onClick={() =>  
            changeOrder(stage.id, stage.title, stage.order - 1)
          }
        >
          <i className="fa fa-caret-up" />
        </div>
        <div
          onClick={() =>
            changeOrder(stage.id, stage.title, stage.order + 1)
          }
        >
          <i className="fa fa-caret-down" />
        </div>
      </div>
    </a>
    <div className="substages-container">
      {sortSubstages.map((substage, index) => (
        <SubstageItem 
          className="substage-container" 
          key={index.toString()} 
          substage={substage} 
          reload={reload} 
        />
      ))}
    </div>
  </div >
4

2 回答 2

2

尝试:

onClick={(e) => {
    e.preventDefault();
    e.stopPropagation();
    changeOrder(stage.id, stage.title, stage.order - 1);
  }
}

于 2020-01-15T13:52:50.987 回答
0

您可以使用 PreventDefault 来避免触发 href 链接。你可以看看https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

HandleChangeOrder = (event,stageid, stagetitle, stageorder) =>{
event.PreventDefault();
//your changeOrder method instructions
}

//Call
<div className="" onClick={() => changeOrder(this.HandleChangeOrder)}>
于 2020-01-15T14:13:02.827 回答