我要实现的效果如下,就是点击链接显示点击对象的文本<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title> new document </title>
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
(function($){
$.fn.myTest = function(options)
{
        var defaults = {
d : "text"
        }
        var config = $.extend(defaults,options); this.click(function()
{
alert("default : " + $(this).text());
return false;
})
}
})(jQuery);$(function()
{
$(".test").myTest();
})
</script>
</head>
<body>
<a href="#" class="test">xxx</a> - <a href="#" class="test">yyy</a>
</body>
</html>我想改成参数调用形式,在jquery插件引入一个参数,然后调用,但是显然下面的代码运行不了,如何改下下面的代码实现上面的效果<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title> new document </title>
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
(function($){
$.fn.myTest = function(options)
{
        var defaults = {
d : "text"
        }
        var config = $.extend(defaults,options); this.click(function()
{
alert("default : " + config.d);
return false;
})
}
})(jQuery);$(function()
{
$(".test").myTest({d:$(this).text()});
})
</script>
</head>
<body>
<a href="#" class="test">xxx</a> - <a href="#" class="test">yyy</a>
</body>
</html>

解决方案 »

  1.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title> new document </title>
    <meta name="author" content="" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
    (function($){
        $.fn.myTest = function(options)
        {
            var defaults = {
                d : "text"
            }
            var config = $.extend(defaults,options);
            this.click(function()
            {
                alert("default : " + config.d);
                return false;
            })
        }
    })(jQuery);$(function()
    {
        $(".test").each(function(){
    $(this).myTest({d:$(this).text()})
    });
    })
    </script>
    </head>
    <body>
    <a href="#" class="test">xxx</a> - <a href="#" class="test">yyy</a>
    </body>
    </html>
      

  2.   

    谢谢楼上的,解决了一个问题,还有个小问题,当数值发生改变后,取到的值还是原来的,请问如何解决这个问题
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title> new document </title>
    <meta name="author" content="" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
    (function($){
        $.fn.myTest = function(options)
        {
            var defaults = {
                d : "text"
            }
            var config = $.extend(defaults,options);        this.click(function()
            {
                alert("default : " + config.d);
    $(this).text("hhhh");
                return false;
            })
        }
    })(jQuery);$(function()
    {
    $(".test").each(function()
    {
    $(this).myTest({d:$(this).text()});
    });
    })
    </script>
    </head>
    <body>
    <a href="#" class="test">xxx</a> - <a href="#" class="test">yyy</a>
    </body>
    </html>
      

  3.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title> new document </title>
    <meta name="author" content="" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
    (function($){
        $.fn.myTest = function(options)
        {
            var defaults = {
                d : "text"
            }
            var config = $.extend(defaults,options);        this.click(function()
            {    
                alert("default : " + config.d);
                alert("now : " + $(this).text());
                $(this).text("hhhh");
                return false;
            })
        }
    })(jQuery);$(function()
    {
        $(".test").each(function()
        {
            $(this).myTest({d:$(this).text()});
        });
    })
    </script>
    </head>
    <body>
    <a href="#" class="test">xxx</a> - <a href="#" class="test">yyy</a>
    </body>
    </html>
      

  4.   

    谢谢楼上的,不是这个意思,我的意思是当点击第一次的时候,是取的点击对象的text值,当第二次点击的时候,text的值发生了变化,但是取出的值还是原来的,应该取出的是发生改变后的值
      

  5.   

    不管值发生多少次变化,取值还是原来的值,应该是当前改变的值<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title> new document </title>
    <meta name="author" content="" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
    (function($){
        $.fn.myTest = function(options)
        {
            var defaults = {
                d : "text"
            }
            var config = $.extend(defaults,options);        this.click(function()
            {   
    $(this).text(parseInt(Math.random()*100));
                alert("default : " + config.d);
                return false;
            })
        }
    })(jQuery);$(function()
    {
        $(".test").each(function()
        {
            $(this).myTest({d:$(this).text()});
        });
    })
    </script>
    </head>
    <body>
    <a href="#" class="test">50</a> - <a href="#" class="test">40</a>
    </body>
    </html>
      

  6.   

    这么多代码。。想挣点分真不容易alert("default : " + config.d);  config.d从来没改变过,当然每次alert出来的结果都是一样的啊。