4

我有一个像这样的javascript原型。

Spry.Widget.TabbedPanels = function(element, opts)
{
    this.element = this.getElement(element);
    this.defaultTab = 0; // Show the first panel by default.
    this.tabSelectedClass = "TabbedPanelsTabSelected";
    this.tabHoverClass = "TabbedPanelsTabHover";
    this.tabFocusedClass = "TabbedPanelsTabFocused";
    this.panelVisibleClass = "TabbedPanelsContentVisible";
    this.focusElement = null;
    this.hasFocus = false;
    this.currentTabIndex = 0;
    this.enableKeyboardNavigation = true;
    this.nextPanelKeyCode = Spry.Widget.TabbedPanels.KEY_RIGHT;
    this.previousPanelKeyCode = Spry.Widget.TabbedPanels.KEY_LEFT;

    Spry.Widget.TabbedPanels.setOptions(this, opts);

    // If the defaultTab is expressed as a number/index, convert
    // it to an element.

    if (typeof (this.defaultTab) == "number")
    {
        if (this.defaultTab < 0)
            this.defaultTab = 0;
        else
        {
            var count = this.getTabbedPanelCount();
            if (this.defaultTab >= count)
                this.defaultTab = (count > 1) ? (count - 1) : 0;
        }

        this.defaultTab = this.getTabs()[this.defaultTab];
    }

    // The defaultTab property is supposed to be the tab element for the tab content
    // to show by default. The caller is allowed to pass in the element itself or the
    // element's id, so we need to convert the current value to an element if necessary.

    if (this.defaultTab)
        this.defaultTab = this.getElement(this.defaultTab);

    this.attachBehaviors();
};

Spry.Widget.TabbedPanels.prototype.getElement = function(ele)
{
    if (ele && typeof ele == "string")
        return document.getElementById(ele);
    return ele;
};

我想从另一个函数更改 defaultTab 的值。那里我用过。

var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");

    alert(TabbedPanels1.defaultTab);
    TabbedPanels1.defaultTab=1;

警报给了我屏幕截图在此处输入图像描述。并且价值没有改变。谁能帮我找出问题所在。

4

2 回答 2

1

我找到了解决方案

TabbedPanels1['defaultTab'].value

这里显示了值,我通过这种方式将其设置为 1。

TabbedPanels1['defaultTab'].value = 1;

这是工作。

于 2013-06-17T15:53:57.067 回答
0

问题是您在执行代码以选择相应元素后更改属性的值。

你应该做类似的事情:

Spry.Widget.TabbedPanels = function(element, opts)
{
    this.element = this.getElement(element);
    this.defaultTab = 0; // Show the first panel by default.
    this.tabSelectedClass = "TabbedPanelsTabSelected";
    this.tabHoverClass = "TabbedPanelsTabHover";
    this.tabFocusedClass = "TabbedPanelsTabFocused";
    this.panelVisibleClass = "TabbedPanelsContentVisible";
    this.focusElement = null;
    this.hasFocus = false;
    this.currentTabIndex = 0;
    this.enableKeyboardNavigation = true;
    this.nextPanelKeyCode = Spry.Widget.TabbedPanels.KEY_RIGHT;
    this.previousPanelKeyCode = Spry.Widget.TabbedPanels.KEY_LEFT;

    Spry.Widget.TabbedPanels.setOptions(this, opts);

    // If the defaultTab is expressed as a number/index, convert
    // it to an element.
    this.setDefaultTab = function(tab){
        this.defaultTab = tab;
        if (typeof (this.defaultTab) == "number")
        {
            if (this.defaultTab < 0)
                this.defaultTab = 0;
            else
            {
                var count = this.getTabbedPanelCount();
                if (this.defaultTab >= count)
                    this.defaultTab = (count > 1) ? (count - 1) : 0;
            }

            this.defaultTab = this.getTabs()[this.defaultTab];
        }

        // The defaultTab property is supposed to be the tab element for the tab content
        // to show by default. The caller is allowed to pass in the element itself or the
        // element's id, so we need to convert the current value to an element if necessary.

        if (this.defaultTab)
            this.defaultTab = this.getElement(this.defaultTab);
    }
    this.setDefaultTab(this.defaultTab);
    this.attachBehaviors();
};

Spry.Widget.TabbedPanels.prototype.getElement = function(ele)
{
    if (ele && typeof ele == "string")
        return document.getElementById(ele);
    return ele;
};

然后您可以使用该功能更改选项卡...

var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");


TabbedPanels1.setDefaultTab(1);
alert(TabbedPanels1.defaultTab);
于 2013-06-17T15:09:55.160 回答