Javascript覆盖rails link_to功能

时间:2015-08-10 17:32:57

标签: javascript jquery ruby-on-rails

所以我在application.html.erb页面上有一些javascript功能。该页面作为正在加载的其他html页面的容器,并且它似乎覆盖了页面上的任何link_to路径和/或href标记或容器内部加载的任何html页面。我的javascript工作正常,我已经安装了jquery-turbolinks gem,所以我怀疑它是一个资产管道问题。

罪魁祸首似乎是以下javascript,因为当我发表评论时,即使我的所有javascript都不再有效,我也可以在rails中再次点击我的链接,一切都正常加载。如果这是一个本质上不成熟的问题,我很抱歉,但我现在对javascript不太满意。任何人都知道为什么会导致这样的问题,解决方案可能是什么?

使用Javascript:

$(function() {
    /* Page Load */
    $('body').keyup(function(event) {

    });

application.html.erb上的Html:

<content class="loadContent">
   <ul>
        <li><%= link_to "login", hud_login_path %></li>
        <li><%= link_to "register", hud_register_path %></li>
   </ul>

<%= yield %>

</content>

完整的javascript:

<script>
$(function() {
    /* Page Load */
    $('body').keyup(function(event) {
        $('div.popup').hide();
        $('div.popup:nth-child('+(event.which-47)+')').show();
        switch(event.which) {
            case 56:
                $('div.popup.friendPics div.friendPicsCont').slimScroll({ 
                    height: '370px',
                    width: '350px',
                    position: 'right',
                    railVisible: false,
                    alwaysVisible: true,
                    railColor: '#4f4c6a',
                    opacity: .7,
                    color: '#4f4c6a'
                });
                break;
        }
    });

    /* Clear Input */
    $('#searchBox').focus(function () {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
        }
    }).blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
        }
    });

    /* Marquee */
    $('#marqueeBlock div.marquee.headline, #adBlock div.marquee.ad').marquee({
        speed: 18000,
        gap: 50,
        delayBeforeStart: 0,
        direction: 'left',
        duplicated: true,
        pauseOnHover: true
    });

    /* Chat Box */
    $('#chatbox').keypress(function(e) {
        if(e.which == 13) {
            $(this).val("");
            /*$('aside#chat .convo').css({'color':'red'});*/
            var chatbox = $('aside#chat .convo');
            $(chatbox)/('<span>Seth:</span><p>Waddup bro!</p>');
            setTimeout(function() {
              $(chatbox).append('<span>Josh:</span><p>Yoo!<br>Whats up?</p>');
            }, 3000);
            setTimeout(function() {
              $(chatbox).append('<span>Seth:</span> <p>Chillin dude<br>wbu?</p>');
            }, 6000);
            setTimeout(function() {
              $(chatbox).append('<span>Josh:</span><p>Not too much man, just downloading some new tunes.</p>').scrollTop($(chatbox)[0].scrollHeight);
            }, 9000);
            setTimeout(function() {
              $(chatbox).append('<span>Seth:</span><p>Dope. Who??</p>').scrollTop($(chatbox)[0].scrollHeight);
            }, 12000);
            setTimeout(function() {
              $(chatbox).append('<span>Josh:</span><p>The new Wolfgang Gartner album :)</p>').scrollTop($(chatbox)[0].scrollHeight);
            }, 15000);
            setTimeout(function() {
              $(chatbox).append('<span>Seth:</span><p>OMG YES!</p>').scrollTop($(chatbox)[0].scrollHeight);
            }, 18000);
            setTimeout(function() {
              $(chatbox).append('<span>Josh:</span><p>Yea, its going down.</p>').scrollTop($(chatbox)[0].scrollHeight);
            }, 21000);
        }
    });

    /* Navigation */
    var wrap = $('content');

    wrap.load('register_temp/');

    /*$('#wheel-link').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#wheel' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });*/
    $('#profileHome-link').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#home' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });
    /* $('#openLib').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#libPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    }); */
    $('#adBlock div.marquee.ad a').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#sponsorAdPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });
    $('.music-search-bar a').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#music-search' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });
    $('#userLibrary .albumPic a').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#artistProfile' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });
    $('#circle a#homebuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#home' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circle a#searchbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#musicSearch' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circle a#prefbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#prefPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circle a#pointsbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#pointsPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circle a#contestsbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#contestsPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circle a#cartbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#artistMerch' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circle a#directorybuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#directoryPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circle a#msgsbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#msgsPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#homebuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#home' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#searchbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#musicSearch' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#prefbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#prefPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#pointsbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#pointsPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#contestsbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#contestsPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#cartbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#artistMerch' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#directorybuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#directoryPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#msgsbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#msgsPage' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#mapbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#artistCareerMap' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#profitsbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#artistProfits' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    $('#circleartist a#statsbuttonLink').on('click', function( e ) {
        var href = $(this).attr('href');
        wrap.load( href + '#artistStats' )
        .hide().delay(100).fadeIn(500);
        e.preventDefault();
    });

    /* Wheel */
    $('#wheel').hide();
    $('#wheelartist').hide();
    $('#wheel-link').on('click', function( e ){
        var pageID = $("div.page:visible");
        var $this = $(this);
        //alert("Visible: " + pageID);
        $(pageID).fadeTo(300, 0.1,function(){
            if ( $this.hasClass('artist') ) {
                $("#wheelartist").fadeIn(300);
            }
            else {
                $("#wheel").fadeIn(300);
            }
        });

        //$("#libPage").fadeIn("slow");
        e.preventDefault();
    });
    $('*').on('click', function( e ){
        var pageID = $("div.page:visible");

        if ($("#wheel, #wheelartist").is(':visible')) {
            $("#wheel, #wheelartist").fadeOut(300, function(){
                $(pageID).fadeTo(300, 1.0);
            });
        }

        e.preventDefault();
    });

    function grow(elem){
        elem.animate({"width" : "+=7", "height":"+=7"}, 200);
    }
    function shrink(elem){
        elem.animate({"width" : "-=7", "height":"-=7"}, 200);
    }
    $('.circle img, .circleartist img').mouseenter(function(){grow($(this));}).mouseleave(function(){shrink($(this));});

    $("img#homebutton").click(function(){
        $("#wheel, #wheelartist").fadeOut("slow");
    });

    $("img#msgsButton").click(function(){
        $("#wheel, #wheelartist").fadeOut("slow");
    });

    $("img#directoryButton").click(function(){
        $("#wheel, #wheelartist").fadeOut("slow");
    });

    $("img#cartButton").click(function(){
        $("#wheel, #wheelartist").fadeOut("slow");
    });

    $("img#contestsButton").click(function(){
        $("#wheel, #wheelartist").fadeOut("slow");
    });

    $("img#pointsButton").click(function(){
        $("#wheel, #wheelartist").fadeOut("slow");
    });

    $("img#prefButton").click(function(){
        $("#wheel, #wheelartist").fadeOut("slow");
    });

    $("img#searchButton").click(function(){
        $("#wheel, #wheelartist").fadeOut("slow");
    });

    $("img#mapButton").click(function(){
        $("#wheelartist").fadeOut("slow");
    });

    $("img#profitsButton").click(function(){
        $("#wheelartist").fadeOut("slow");
    });

    $("img#statsButton").click(function(){
        $("#wheelartist").fadeOut("slow");
    });

    /* Search */
    $('#search-suggestions').on('click', function( e ){
        $('#search-suggestions').toggle();
        $('#searchBox').val('Search Friends, Artists, and Songs');
    });

    /* Library */
    $('#libPage').hide();
    $('#openLib, #home a.libraryButton, #profile a.libraryButton').on('click', function( e ){
        var pageID = $("div.page:visible");

        $(pageID).fadeTo(300, 0.05,function(){
            $("#libPage").fadeIn(300);
        });

        e.preventDefault();
    });
    $('#userLibrary a, #userLibrary .libraryCloseX a').on('click', function( e ){
        var pageID = $("div.page:visible");

        $("#libPage").fadeOut(300, function(){
            $(pageID).fadeTo(300, 1.0);
        });

        e.preventDefault();
    });
    $('.libraryScroll1').slimScroll({ 
        height: '380px',
        width: '650px',
        position: 'right',
        railVisible: true,
        alwaysVisible: true,
        //railColor: '#4f4c6a',
        opacity: .7,
        color: '#4f4c6a'
    });
    $('.librarybox2').slimScroll({ 
        height: '380px',
        width: '180px',
        position: 'right',
        railVisible: true,
        alwaysVisible: true,
        //railColor: '#4f4c6a',
        opacity: .7,
        color: '#4f4c6a'
    });

    /* Player */
    /* $('a.musicNote').click(function(){
        var pageID = $("div.page:visible");

        //alert("Visible: " + pageID);
        $(pageID).fadeOut("slow",function(){
            $("#libPage").fadeIn("slow");
        });
    }); */

    /* Radio */
    $('.radio').css({ 
        'display':'none'
    });
    $('a.radioTower').on('click', function( e ){
        var pageID = $("div.page:visible");

        $(pageID).fadeTo(300, 0.1, function(){
            $('.radio').fadeIn(300);
        });

        e.preventDefault();
    });
    $('.radio img.image').on('click', function( e ){
        var pageID = $("div.page:visible");

        $('.radio').fadeOut(300, function(){
            $(pageID).fadeTo(300, 1.0);
        });

        e.preventDefault();
    });

    /* Live Listening */
    $('.live-listening').css({ 
        'display':'none'
    });
    $('.icon-live-listening img').on('click', function( e ){
        var pageID = $("div.page:visible");

        $(pageID).fadeTo(300, 0.1, function(){
            $('.live-listening').fadeIn(300);
        });

        e.preventDefault();
    });
    $('.live-listening img.image').on('click', function( e ){
        var pageID = $("div.page:visible");

        $('.live-listening').fadeOut(300, function(){
            $(pageID).fadeTo(300, 1.0);
        });

        e.preventDefault();
    });
});
</script>

这似乎是罪魁祸首。有谁知道为什么这会覆盖默认功能?

/* Wheel */
    $('#wheel').hide();
    $('#wheelartist').hide();
    $('#wheel-link').on('click', function( e ){
        var pageID = $("div.page:visible");
        var $this = $(this);
        //alert("Visible: " + pageID);
        $(pageID).fadeTo(300, 0.1,function(){
            if ( $this.hasClass('artist') ) {
                $("#wheelartist").fadeIn(300);
            }
            else {
                $("#wheel").fadeIn(300);
            }
        });

        //$("#libPage").fadeIn("slow");
        e.preventDefault();
    });
    $('*').on('click', function( e ){
        var pageID = $("div.page:visible");

        if ($("#wheel, #wheelartist").is(':visible')) {
            $("#wheel, #wheelartist").fadeOut(300, function(){
                $(pageID).fadeTo(300, 1.0);
            });
        }

        e.preventDefault();
    });

0 个答案:

没有答案
相关问题