jquery 插件出错



作为初学者,我的自定义插件出现错误。我正在尝试更改页面上div 的属性。

这是我的JavaScript代码:

(function($) {
$.fn.changeDiv = function( options ) {
    var settings = $.extend({
        // These are the defaults.
        text  : 'Hello, World!',
        color: "#556b2f",
        backgroundColor: "white",
        borderColor : "white",
        borderWeight: "1px;",
        fontWeight   : 'bold',
        fontStyle   : 'Trebuchet MS, Callibri, Sans-Serif',
        fontSize  : '18px',
        position: "center",
        fl : "auto",
        wt: "auto",
        ht: "auto",
        mRight: "auto",
        mLeft: "auto",
        complete : null
    }, options );
            return this.each( function() {
         $(this).text( settings.text );
        if ( settings.color ) {
            $(this).css( 'color', settings.color );
        }
        if ( settings.fontStyle ) {
            $(this).css( 'font-style', settings.fontStyle );
        }
        if ( settings.fontWeight ) {
            $(this).css( 'font-weight', settings.fontWeight );
        }
        if ( settings.fontSize ) {
            $(this).css( 'font-size', settings.fontSize );
        }
        if ( settings.wt ) {
            $(this).css( 'width', settings.wt );
        }
        if ( settings.ht ) {
            $(this).css( 'height', settings.ht );
        }
        if ( settings.position ) {
            $(this).css( 'text-align', settings.position );
        }
        if ( settings.fl ) {
            $(this).css( 'float', settings.fl );
        }
        if ( settings.mRight ) {
            $(this).css( 'margin-right', settings.mRight );
        }
        if ( settings.mLeft ) {
            $(this).css( 'margin-left', settings.mLeft );
        }
        if ( $.isFunction( settings.complete ) ) {
            settings.complete.call( this );
        }
  });
};

我在调用函数时使用它:

$('#header').changeDiv({
text: 'This is the header div',
    fontStyle: 'Verdana, Arial, Helvetica, Sans-serif',
fontWeight: 'bold',
backgroundColor: '#406481',
fontSize: '30px',
ht: "50px",
position: "center",
complete    : function() { $(this).easeIn("slow",1500); }
});

我得到的结果只是带有绿色和中心对齐的文本"这是标题div"。如果我将相同的 changeDiv 函数应用于菜单,则没有任何反应。如果我将返回函数更改为以下内容:

return this.css({
    color: settings.color,
    backgroundColor: settings.backgroundColor,
    borderColor: settings.backgroundColor,
    borderWeight: settings.borderWeight,
fontWeight: settings.fontWeight,
fontStyle: settings.fontStyle,
fontSize: settings.fontSize,
position: settings.position,
fl: settings.fl,
wt: settings.wt,
ht: settings.ht,
mRight:settings.mRight,
mLeft: settings.mLeft
});

我可以在 FireBug 中看到样式应用于div,但默认文本不会更改。

帮助!

您必须在


    $(function($) {

所以你需要 $ 字符。(在您的第一行)当你称它为chageDiv时,也使用这个定义。它在jsFiddle中对我有用。

我认为解决方案在于您尝试编写的cssfontStyle必须font-family.

试试这个:

$(this).css( 'font-family', settings.fontStyle );

编辑

问题已经解决了:其他人试图找到答案的工作示例:

在第 1 行中添加了 $,并对原始代码进行了一些更改

http://jsfiddle.net/e5HHc/1/

相关内容

最新更新