简体   繁体   English

更改类时更改href函数jquery

[英]Change a href function when class is changed jquery

I have a href link which will connect or disconnect a user. 我有一个href链接,它将连接或断开用户连接。
When the user is connected the link must disconnect him when it is clicked, in another hand, it must connect the user if the user is not connected yet. 连接用户后,链接必须在单击时断开与用户的连接,另一方面,如果用户尚未连接,则必须连接用户。
Everything goes fine with me but when I disconnect a user I cannot connect him until I refresh the page. 一切对我来说都很好,但是当我断开用户连接时,直到刷新页面后我才能与他建立连接。 I have this javascript code : 我有这个javascript代码:

$(document).ready(function() {
    $('a.disconect').click(function(e) {
        e.preventDefault();
        $(this).css('background', 'url(images/connect.png) no-repeat');
        $(this).removeClass('disconect');
        $(this).addClass('connect');
        //: url(images/rep_usr.png) no-repeat;
        var parent = $(this).parent().parent();
        $.ajax({
            type: 'get'
            , url: 'Save.php?id=enable'
            , data: 'username=' + $(this).attr('id').replace('record-', '')
            , beforeSend: function(data) {
                //alert(data);
            }
            , success: function(data) {
                //alert(data);
            }
        });
    });
});   


$(document).ready(function() {
    $('a.connect').click(function(e) {
        e.preventDefault();

        $(this).css('background', 'url(images/disconnect.png) no-repeat');
        $(this).removeClass('connect');
        $(this).addClass('disconect');
        //: url(images/rep_usr.png) no-repeat;
        var parent = $(this).parent().parent();
        $.ajax({
            type: 'get'
            , url: 'Save.php?id=disable'
            , data: 'username=' + $(this).attr('id').replace('record-', '')
            , beforeSend: function(data) { //alert(data);
                //alert(data);
            }
            , success: function(data) {
                //alert(data);
            }
        });
    });
});

I want to call another function every time it is clicked. 我想在每次单击时调用另一个函数。

You are trying to dynamically change the class, which then does not map to the events that fire on those classes. 您试图动态更改该类,然后该类不会映射到在这些类上触发的事件。 First, you should be using 首先,您应该使用

$(document).on('click', 'a.disconect', function(){

instead of 代替

$('a.disconect').click(function(e) {

as this will dynamically map the events to any new classes that are added to the DOM. 因为这会将事件动态映射到添加到DOM的任何新类。 https://learn.jquery.com/events/event-delegation/ https://learn.jquery.com/events/event-delegation/

But a better solution is to create two buttons and .hide() or .show() whichever one you want to be active. 但是,更好的解决方案是创建两个按钮以及.hide()或.show(),无论您要激活哪个按钮。 Then you don't have to go through the unnecessary adding-and-removing of classes and background images. 然后,您不必经历不必要的类和背景图像的添加和删除。

Hello If i understand your problem clearly, then you can use hasClass() function to find the current class then change that i added a similar code below hope you will find something helpful through it 您好,如果我清楚地理解了您的问题,那么您可以使用hasClass()函数查找当前类,然后更改为我在下面添加了类似的代码,希望对您有所帮助

 $("a").click(function(){ if($(this).hasClass("current")||$(this).hasClass("disconnect") ) { $(this).removeClass("current"); $(this).removeClass("disconnect"); $(this).addClass("connect"); $(this).text("You are Connected") } else { $(this).removeClass("current"); $(this).removeClass("connect"); $(this).addClass("disconnect"); $(this).text("You are dis Connected") } }); 
 .connect { font-size: 150%; color: green; } .disconnect{ font-size: 150%; color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <a class="current" href="#">No Line</a> <p> </p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM