---
title: "通过$.data判断，并执行函数"
author: "keimon"
date: "2013-08-27T09:43:57Z"
tags: ["jQuery"]
categories: ["Web前端"]
slug: "data"
draft: false
lastmod: "2013-08-27T09:48:50Z"
id: 1931395
url: "https://keimon.iteye.com/blog/1931395"
---

&lt;select id="s"&gt;  
  &lt;option&gt;1&lt;/option&gt;  
  &lt;option&gt;2&lt;/option&gt;  
&lt;/select&gt;  
&lt;script type="text/javascript" src="jquery-1.9.1.js"&gt;&lt;/script&gt;  
&lt;script&gt;  
//通过$.data判断，并执行函数；  
var defaults = {  
        name:'a',  
        age:'29'  
    }  
function Fun(ele, opt){  
    this.ele = ele;  
    this.opt = $.extend({}, defaults, opt);  
    this.init();  
}  
Fun.prototype = {  
    init:function(){  
        console.log(this.opt.age);  
        console.log(this.opt.name);

        console.log(this.ele.children.length);  
    }  
}  
     
$.fn.test = function(opts){  
    if(!$.data(this, 'plugin\_aa')){  
        $.data(this, 'plugin\_aa', new Fun(this, opts));   //通过$.data执行函数（new Fun()）  
    }  
}  
$('#s').test();  
&lt;/script&gt;