为什么我的 Javascript 函数只能运行一段时间?

发布于 2024-09-19 20:56:32 字数 1489 浏览 4 评论 0原文

我的沙箱在这里:http://9.latest.truxmap.appspot.com/

首先单击地图上的标记(如果页面加载时没有标记,请选中导航小部件中的“食品卡车即将开业”框)。接下来转到“评论”选项卡。

在 GWT 中,每次打开标记时,我都会调用 javascript 函数 resizeReviewTab(),该函数可以纠正由于状态和审阅选项卡中的内容是动态的而出现的样式问题。如果 Javascript 有效,那么您将看到“评论”选项卡中文本区域正上方漂浮着星星,而不是单选按钮。否则,您将看到普通的单选按钮。

我不知道当标记不能正确打开时会发生什么。它怎么可能一次起作用,然后不再起作用,然后在几个不同的标记之后再次起作用?下面是这个函数:

function resizeReviewTab(){ 

    $('#content').text(""); 

    $('.statusWindowB').css('height', $('.statusWindowA').css('height'));                   

    $('.name-sliding').focus(function() {

        $('.name-label-sliding').animate({ marginLeft: "133px" }, "fast");

            if($(this).val() == "name")
                $(this).val() == "";

        }).blur(function() {

            if($(this).val() == "") {
                $(this).val() == "name";
                $('.name-label-sliding').animate({ marginLeft: "12px" }, "fast");
            }
        });     

    $('.content-sliding').focus(function() {
            $('.content-label-sliding').fadeOut("slow");
    });

    starify();  
} 

starify() 是 jQuery 插件 jquery.stars 的 javascript,做了一些修改,如下所示: http://9.latest.truxmap.appspot.com/lib/jquery. rating.js

我需要调用这个函数,因为如果我只是在在 html 文档的开头,通过单击地图创建的任何信息窗口都不会将其单选按钮更改为星形。

这个问题有点麻烦,期待您的回复。谢谢!

My sandbox is here: http://9.latest.truxmap.appspot.com/

First click a marker on the map (if there are none when the page loads, check the 'food trucks opening soon' box in the navigation widget). Next go to the 'Reviews' tab.

In GWT, everytime I open a marker I'm calling the javascript function resizeReviewTab() which corrects styling issue that arise given the fact that the content in the status and review tabs are dynamic. If the Javascript worked, then you will see STARS rather than radio buttons floating right above the text area in the 'Reviews' tab. Otherwise, you'll see plain radio buttons.

I cant figure out what happens when the marker DOESNT open up properly. How could it work once, then not again, then work again after a few different markers? Heres the function thats called:

function resizeReviewTab(){ 

    $('#content').text(""); 

    $('.statusWindowB').css('height', $('.statusWindowA').css('height'));                   

    $('.name-sliding').focus(function() {

        $('.name-label-sliding').animate({ marginLeft: "133px" }, "fast");

            if($(this).val() == "name")
                $(this).val() == "";

        }).blur(function() {

            if($(this).val() == "") {
                $(this).val() == "name";
                $('.name-label-sliding').animate({ marginLeft: "12px" }, "fast");
            }
        });     

    $('.content-sliding').focus(function() {
            $('.content-label-sliding').fadeOut("slow");
    });

    starify();  
} 

starify() is the javascript for the jQuery plugin jquery.stars with a few modifications, visible here: http://9.latest.truxmap.appspot.com/lib/jquery.rating.js

I need to call this function because if I merely load it at the beginning of the html document, none of the info windows created by clicking the map will ever have their radio buttons changed into stars.

This is quite bothersome, I'm looking forward to your replies. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我最亲爱的 2024-09-26 20:56:32

这些位肯定坏了

if($(this).val() == "name")
    // next line is checking whether val equals ""
    // we already know it equals "name"
    // so it returns false(which is discarded)
    $(this).val() == ""; "name" ,

if($(this).val() == "") {
    // next line is checking whether val equals "name"
    // we already know it equals ""
    // so it returns false(which is discarded)
    $(this).val() == "name";
    $('.name-label-sliding').animate({ marginLeft: "12px" }, "fast");
}

想你的意思

if($(this).val() == "name")
    $(this).val(''); // sets val to nothing

if($(this).val() == "") {
    $(this).val("name"); // sets val to "name"
    $('.name-label-sliding').animate({ marginLeft: "12px" }, "fast");
}

These bits are definitely broken

if($(this).val() == "name")
    // next line is checking whether val equals ""
    // we already know it equals "name"
    // so it returns false(which is discarded)
    $(this).val() == ""; "name" ,

And

if($(this).val() == "") {
    // next line is checking whether val equals "name"
    // we already know it equals ""
    // so it returns false(which is discarded)
    $(this).val() == "name";
    $('.name-label-sliding').animate({ marginLeft: "12px" }, "fast");
}

I think you meant

if($(this).val() == "name")
    $(this).val(''); // sets val to nothing

And

if($(this).val() == "") {
    $(this).val("name"); // sets val to "name"
    $('.name-label-sliding').animate({ marginLeft: "12px" }, "fast");
}
看春风乍起 2024-09-26 20:56:32

@amurra 和 @meouw 给出的两个答案是有效的,并且可能会在将来引起问题。然而,OP中提出的问题的答案是,每次活动单元格发生变化时,GWT都会调用SelectionChangedHandler两次——一次是在取消选择一个单元格时,一次是在另一个单元格被选中时。重新选择。这导致 javascript 函数被连续调用两次,从而导致令人讨厌的行为。非常感谢您在此线程中的帮助!

The two answers given by @amurra and @meouw were valid and could have caused issues in the future. However, the answer to the question asked the in OP is that GWT calls the SelectionChangedHandler TWICE, every time the active cell is changed -- once when a cell is de-selected, and once when another cell is re-selected. This was causing the javascript function to be called twice, back-to-back, which was leading to nasty behavior. Thanks a lot for the help in this thread!

赠佳期 2024-09-26 20:56:32

如果函数被多次调用,您可能需要在绑定之前取消绑定事件。这将有助于防止多次绑定,从而导致奇怪且不一致的结果。

function resizeReviewTab(){ 

$('#content').text(""); 

$('.statusWindowB').css('height',$('.statusWindowA').css('height'));                   

$('.name-sliding').unbind("focus").focus(function() {

    $('.name-label-sliding').animate({ marginLeft: "133px" }, "fast");

        if($(this).val() == "name")
            $(this).val() == "";

    }).unbind("blur").blur(function() {

        if($(this).val() == "") {
            $(this).val() == "name";
            $('.name-label-sliding').animate({ marginLeft: "12px" }, "fast");
        }
    });     

$('.content-sliding').unbind("focus").focus(function() {
        $('.content-label-sliding').fadeOut("slow");
});

starify();  

}

You might want to unbind your events before binding if a function gets called multiple times. This will help prevent multiple binds, which can cause weird and inconsistent results.

function resizeReviewTab(){ 

$('#content').text(""); 

$('.statusWindowB').css('height',$('.statusWindowA').css('height'));                   

$('.name-sliding').unbind("focus").focus(function() {

    $('.name-label-sliding').animate({ marginLeft: "133px" }, "fast");

        if($(this).val() == "name")
            $(this).val() == "";

    }).unbind("blur").blur(function() {

        if($(this).val() == "") {
            $(this).val() == "name";
            $('.name-label-sliding').animate({ marginLeft: "12px" }, "fast");
        }
    });     

$('.content-sliding').unbind("focus").focus(function() {
        $('.content-label-sliding').fadeOut("slow");
});

starify();  

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文