json上的jquery事件通过爆米花解析

时间:2012-12-03 16:39:04

标签: jquery json popcornjs

有谁知道为什么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."
        }
    }
  ]
}

我真的很感激任何帮助!

2 个答案:

答案 0 :(得分:2)

将委托与.on()

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

用最接近的祖先替换'body'(不必更高效),并且在dom准备就绪时存在。这会将事件处理程序绑定到该元素 - 当它从给定的选择器冒泡时,它会监听事件。

答案 1 :(得分:1)

尝试在parseJSON或其他ajax衍生产品的回调函数中使用jQuery .on()

$("a[href='gohere.html']").on('click', function() {
       alert("clicked!");        
 });