0

有谁知道为什么 jquery 事件不适用于通过 Popcorn.js JSON Parser 解析的 json 数据?

单击链接时,我想调用一个函数。有问题的链接在 json 文件中。该文件似乎按预期进行了解析,并且爆米花脚注按预期显示。我可以关注已解析的链接,但没有触发任何事件。

这是我正在处理的一段代码:

<script>
$(function() {

     var popcorn = Popcorn("#video");
     popcorn.parseJSON ("data/data.json");

     $("a[href='gohere.html']").click(function() {
           alert("clicked!"); //should display the message, but doesn't         
     });
});
</script>

JSON 文件如下所示:

{
 "data": [
    {
        "footnote": {
            "start":"1",
            "end":"3",
            "target":"footnote-container",
            "text":"<a href='gohere.html'>Go Here</a>"
        }
    },
    {
        "footnote": {
            "start":4,
            "end":7,
            "target":"footnote-container",
            "text":"Another piece of text."
        }
    }
  ]
}

我真的很感激任何帮助!

4

2 回答 2

2

将委托与.on()一起使用

 $('body')on('click','a[href="gohere.html"]',function() {
       alert("clicked!"); //should display the message, but doesn't         
 });

用最近的静态祖先替换'body'(不是必需的,但更有效),并且在 dom 准备好时存在。这会将事件处理程序绑定到该元素 - 当事件从给定的选择器中冒出来时,它会依次侦听该事件。

于 2012-12-03T17:00:08.900 回答
1

Try using jQuery .on() inside a callback function from parseJSON or some other ajax derivative.

$("a[href='gohere.html']").on('click', function() {
       alert("clicked!");        
 });
于 2012-12-03T16:42:00.173 回答