将按钮href更改为页面上的其他href

时间:2016-03-18 16:47:53

标签: jquery html button href

我的网站有退出链接。

<a href="http://specialuniquelogout.com">Logout</a>

当有人点击此链接时,会弹出一个通知框,询问您&#34;您确定要退出吗?&#34;。他们可以按是或否。如果他们按否,则关闭通知框。但是我需要动态地将YES按钮的href属性设置为他们单击的原始注销链接的href。

我在想也许我可以某种方式使用window.location = $this.attr('href'); 但无法弄明白。

这是我的代码:

<a class="mb-control" data-box="#mb-signout" href="http://UNIQUELOGOUTLINK.com">Logout</a>

<!-- MESSAGE BOX-->
        <div class="message-box animated fadeIn" data-sound="alert" id="mb-signout">
            <div class="mb-container">
                <div class="mb-middle">
                    <div class="mb-title"><span class="fa fa-sign-out"></span> Log <strong>Out</strong> ?</div>
                    <div class="mb-content">
                        <p>Are you sure you want to log out?</p>                    
                        <p>Press No if you want to continue work. Press Yes to logout current user.</p>
                    </div>
                    <div class="mb-footer">
                        <div class="pull-right">
                            <a href="NEED TO PUT THE UNIQUE LOGOUT LINK HERE" class="btn btn-success btn-lg">Yes</a>
                            <button class="btn btn-default btn-lg mb-control-close">No</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- END MESSAGE BOX-->

        <!-- START PRELOADS -->
        <audio id="audio-alert" src="audio/alert.mp3" preload="auto"></audio>
        <audio id="audio-fail" src="audio/fail.mp3" preload="auto"></audio>
        <!-- END PRELOADS -->   

<script>

/* PLAY SOUND FUNCTION */
function playAudio(file){
    if(file === 'alert')
        document.getElementById('audio-alert').play();

    if(file === 'fail')
        document.getElementById('audio-fail').play();    
}
/* END PLAY SOUND FUNCTION */

   /* MESSAGE BOX */
    jQuery(".mb-control").on("click",function(){
        var box = $($(this).data("box"));
        if(box.length > 0){
            box.toggleClass("open");

            var sound = box.data("sound");

            if(sound === 'alert')
                playAudio('alert');

            if(sound === 'fail')
                playAudio('fail');

        }        
        return false;
    });
    jQuery(".mb-control-close").on("click",function(){
       $(this).parents(".message-box").removeClass("open");
       return false;
    });    
    /* END MESSAGE BOX */

</script>

<style>
.message-box {
    display: none;
    position: fixed;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9999;
}

.message-box.open {
    display: block;
}

</style>

感谢任何可以提供帮助的人!

1 个答案:

答案 0 :(得分:1)

可能是这样的:

jQuery(".mb-control").on("click", function() {
  var box = $($(this).data("box"));
  if (box.length > 0) {
    box.toggleClass("open");
    box.find('.btn-close').attr('href', this.href);

    var sound = box.data("sound");

    if (sound === 'alert')
      playAudio('alert');

    if (sound === 'fail')
      playAudio('fail');
  }
  return false;
});

我在课程中添加了课程btn-close

<a href="NEED TO PUT THE UNIQUE LOGOUT LINK HERE" class="btn btn-success btn-lg btn-close">Yes</a>
相关问题