Facebook JS (FBJS) 事件监听器

发布于 2024-09-30 00:37:48 字数 1045 浏览 2 评论 0原文

所以我浏览了这里的大部分 Facebook 问题,它绝对证实了我的想法。 Facebook 开发可能是我用过的最糟糕的开发之一。我现在会避免我的咆哮,但作为我来自哪里的注释:尝试了 php sdk,开箱即用,发现我需要将功能放在页面选项卡上,不能使用 iframe页面选项卡,必须使用 FBML(它们将在两个月后退休,但我还无法开始测试 iframe 方法)

无论如何,我此时遇到了 FBJS。它对于大多数事情来说“足够好”,但由于某种原因,我无法通过添加侦听器(根据 FBJS 文档)将事件注册到对象(特别感兴趣的选择框)。我可以将它直接添加到 html 对象并让它工作,但这并不可取,我真的很想知道我认为正确的方法是什么,这涉及逻辑和显示的分离。

我想要发生的事情:单击选择框,进行选择,在空 div 中显示选择的文本(稍后添加 Ajax,但在这里一次一步)

代码:

<script>
    var obj = document.getElementById('select-id');
    obj.addEventListener('onchange',my_func);
    function my_func(evt){
        var inner = document.getElementById('div-id');
        inner.setTextValue('hey');        // for testing purposes
    }
</script>

上面的代码在以下情况下不执行任何操作我对选择框进行了更改。然而,这按计划进行:

<select name="find_state" id="find_state" onchange="my_func();">

我在开发时会勉强使用这种方法,但真的很想知道我可能做错了什么或者其他人是否有这个问题?如果有人对此事有任何意见,我很想知道某种形式的 Facebook 开发建议,因为应用程序、页面和选项卡的行为似乎完全不同,但似乎它们都应该做同样的事情我?有没有统一的方法来开发这三件事,或者我错过了什么?

预先感谢以及过去的帮助!

So I have looked through most of the facebook questions here and it has absolutely confirmed my thoughts. Facebook development may be among some of the worst I've ever used. I'll avoid my rant for now, but as a note to where I'm coming from: tried php sdk, worked decently out of the box, found out I need to put the functionality on a pages tab, can't iframe on pages tab, have to use FBML (which they are retiring in two months, yet I can't start testing the iframe approach yet)

Anyway, I run into FBJS at this point. It works "good enough" for most things, but for some reason I can't get an event registered to an object (a select box in particular interest) by adding a listener (as per FBJS documentation). I am able to add it directly to the html object and have it work, but this is not desirable and I would really like to know what I consider the proper way to do it, which involves seperation of logic and display.

What I want to happen: click on the select box, make a selection, display the text of the selection in an empty div (later on adding Ajax but one step at a time here)

Code:

<script>
    var obj = document.getElementById('select-id');
    obj.addEventListener('onchange',my_func);
    function my_func(evt){
        var inner = document.getElementById('div-id');
        inner.setTextValue('hey');        // for testing purposes
    }
</script>

The above code doesn't do anything when I make a change to the select box. However, this behaves as planned:

<select name="find_state" id="find_state" onchange="my_func();">

I will be grudgingly using this method as I develop, but would really love to know what I might be doing wrong or if anyone else has this issue? And if anyone has any opinions on the matter I would love to know of some form of facebook development recommendations as applications, pages, and tabs all appear to behave totally different from eachother, yet it seems that they all should be doing the same thing to me? Is there any unified way to develop across all three of these things, or am I missing something?

Thanks in advance, as well as for the past help!

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

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

发布评论

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

评论(3

独夜无伴 2024-10-07 00:37:49

使用以下 html 和 .addEventListener() 附加的事件开始工作。这似乎是未记录的“功能”。

<select name="find_state" id="find_state" onmousedown="return true;">

这也使得该事件能够在用户第一次更改 select 的值时触发。否则它只会在第二个 onchange 事件时触发。

Use the following html and your events attached with .addEventListener() start to work. This seems to be undocumented "feature".

<select name="find_state" id="find_state" onmousedown="return true;">

This also enables the event to fire first time the user changes the value of select. Otherwise it would fire only on second onchange event.

天冷不及心凉 2024-10-07 00:37:48

我认为应该是:(

obj.addEventListener('change',my_func);

而不是onchange

I think it should be:

obj.addEventListener('change',my_func);

(instead of onchange)

谎言 2024-10-07 00:37:48

直接来自 Facebook 文档

第三个​​参数 [addEventListener],需要布尔值 useCapture(它没有默认值)

这意味着您应该:

obj.addEventListener('change', my_func, false);

Straight from Facebook documentation:

The third parameter [to addEventListener], boolean useCapture is required (it does not have a default value)

That means that you should have:

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