我试图检测超链接的文本何时发生变化(基于我无法控制的javascript函数)。
当文字更改为'与'不同时,我想附加点击事件。为了简化这个问题,我只需要检测文本何时被更改为'与'不同。我正在尝试以下操作,但它会在文本更改之前触发:
$("a:contains('Like')").click(function(){LikeClick($(this))});
function LikeClick() {
$("#" + ths[0].id + ":contains('Unlike')").ready(function(){
alert('the text has been changed')
//Attach click event to all like buttons
});
}
这是原始元素:
<a href="javascript:;" id="likesElement-1" class="ms-secondaryCommandLink">Like</a>
答案 0 :(得分:0)
这样的东西?
$('.like').on('click', function(){
//whatever you want the code to do on a like.
//If it has a callback function wrap the code below in that.
this.removeClass('like');
this.addClass('unlike');
this.text("Unlike");
});
$('.unlike').on('click', function(){
// unlike code goes here. Same as before with callback.
this.removeClass('unlike');
this.addClass('like');
this.text("Like");
});
只需在您喜欢的链接/按钮上添加like
课程即可。您可能需要将prevent default添加到这些函数的开头。
答案 1 :(得分:0)
这似乎是解决问题的简单方法:
$('.theButton').on('click',function(){
//Toggle the 'like' class on each click, then look for the class
if($(this).hasClass('like')){
$(this).removeClass('like').addClass('unlike');
likeClick();
} else {
$(this).removeClass('unlike').addClass('like');
unlikeClick();
}
}