javascript - 无法设置/读取未定义的属性

标签 javascript css styles element

我正在尝试制作一个“站点布局”类型的东西,您可以在其中单击一个属性和一个值,然后面板会更改为那个东西。我需要能够拥有一系列可以点击的界面。

我遇到错误无法设置/读取未定义的属性颜色

这是我的代码:

var panel = "";
var colorType = "texts";
var interfaces = new Array();
interfaces.push(new Interface());
var colors = 'black'
var idx = 0;

var interfaces = new Array();

function Interface(c)
{
    this.colors = c;
}
function changeColor()
{
    if(panel != '')
    {
        if(colorType == 'texts')
        {
            document.getElementById(panel).style.color = interfaces[idx].colors;
        }
        if(colorType == 'background')
        {
            document.getElementById(panel).style.backgroundColor = interfaces[idx].colors;
        }
    }
}
function changeSize()
{
    document.getElementById(panel).style.fontSize = interfaces[idx].getElementById('fsizes').value +'px';
}
function panelClicked()
{
    document.getElementById('texts').style.borderStyle = 'solid'; 
    document.getElementById('background').style.borderStyle = 'none'; 
    colors = 'black'; 
    colorType = 'texts';
    changeColor();
}

这是造成问题的 HTML:

<table style = "width:90px;">
            <tr>
                <td class = 'colors' style = "background-color:red;" onclick = "interfaces[idx].colors = 'red'; changeColor();"></td>
                <td class = 'colors' style = "background-color:orange;" onclick = "interfaces[idx].colors = 'orange'; changeColor();"></td>
                <td class = 'colors' style = "background-color:yellow;" onclick = "interfaces[idx].colors = 'yellow'; changeColor();"></td>
            </tr>
            <tr>
                <td class = 'colors' style = "background-color:green;" onclick = "interfaces[idx].colors = 'green'; changeColor();"></td>
                <td class = 'colors' style = "background-color:blue;" onclick = "interfaces[idx].colors = 'blue'; changeColor();"></td>
                <td class = 'colors' style = "background-color:purple;" onclick = "interfaces[idx].colors = 'purple'; changeColor();"></td>
            </tr>
            <tr>
                <td class = 'colors' style = "background-color:lightpink;" onclick = "interfaces[idx].colors = 'lightpink '; changeColor();"></td>
                <td class = 'colors' style = "background-color:thistle;" onclick = "interfaces[idx].colors = 'thistle'; changeColor();"></td>
                <td class = 'colors' style = "background-color:black;" onclick = "interfaces[idx].colors = 'black'; changeColor();"></td>
            </tr>
        </table>

最佳答案

您调用“var interfaces = new Array()”两次。如果你把它取出来,你应该没问题。

关于javascript - 无法设置/读取未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9865970/

相关文章:

javascript - 替换字符串中的最后一个数字

javascript - 如何使用 jquery 显示当前日期以及时间 AM PM 和 CST

javascript - 使用数组 Javascript 制作幻灯片

javascript - 浏览器检测

android - 使用自定义属性创建默认样式

html - flexbox 订单但从新行开始?

javascript - 如何使用 jQuery 通过悬停来仅显示第一个?

html - 使用第三方 CSS 有危险吗?

python - 如何在PyQt中获取Qtwidgets的原生样式?

css - 如何从 QPushButton 中删除点击效果