我有一个HTML表,在该表的行上附加了click事件,一旦单击行,就会切换“活动”类(添加和删除)。
此表内有链接带有附加的单击事件。
是否有一种方法可以防止那些链接单击事件触发行类?
$("table tbody tr").on("click", function(){
$(this).toggleClass('active');
});
$("table tbody a").on("click", function(){
//do something else but don't toggle the active class
});
答案 0 :(得分:0)
您可以在//I added in constructor
this.state: dataPost: '',
//next I create function to load data and send to state dataPost
handleData = (e) => {
const {posts} = this.props;
const {dataPost} = this.state;
const letang = posts;
const postsData = dataPost;
if (postsData.length <= 0) {
this.setState({
dataPost: letang
})
} else {
console.log('stop')
}
}
// next i create in componentDidUpdate this code
componentDidUpdate(prevProps, prevState) {
const {posts} = this.props;
const indexPosts = posts.findIndex((post) => post.id === this.state.postId);
if(prevProps.posts !== posts){
this.handleData();
} else if (indexPosts !== -1)
{
this.informationAlert();
const log = posts.splice(indexPosts, 1);
console.log(log);
}
}
点击事件上调用stopPropagation()
,以防止其将DOM传播到父a
:
tr