jquery
  • plugins
  • selector
  • this
  • 2013-07-07 31 views 1 likes 
    1

    我寫了一個簡單的彈出式插件,簡單地彈出一個div和黑屏。它以兩種方式工作。簡單的消息和加載ajax。這裏是我的問題訪問當前選定的元素與選項

    我有這樣的民意調查的名單:

    <div id="pollList" style=" margin-top:10px;"> 
    <li pollId='1'><strong>polling number 1</strong></a><br /><span style='font-size:9px;'>this is a test polling</span></li> 
    <li pollId='2'><strong>polling number 2</strong></a><br /><span style='font-size:9px;'>this is a test polling</span></li> 
    

    現在我想,我的插件適用於所有這李標籤,所以我用這個:

    $("#pollList li").popup({ 
        width:800, 
        height:400, 
        popupType:'ajax', 
        ajaxURL:root+'polls/attend/poll/'+$(this).attr('pollId') 
    }); 
    

    這裏是我想通過$(this).attr('pollId')到插件來調用相關的URL,但$(this)似乎不工作,並返回'undefined'我怎麼能訪問pollId屬性惠特我n插件選項?

    回答

    0

    您有的this可能是全局對象,或者如果您在事件處理程序中綁定了處理程序的對象。無論如何,要獲得適當的上下文,您可以使用每個函數根據具體的li來設置url。

    $("#pollList li").each(function(){ 
        $(this).popup({ 
        width:800, 
        height:400, 
        popupType:'ajax', 
        ajaxURL:root+'polls/attend/poll/'+$(this).attr('pollId') 
        }); 
    }); 
    

    此外,您可能想要使用數據屬性而不是任意數據屬性。

    +0

    謝謝,這是工作 –

    相關問題