javascript 未捕获的类型错误:字符串不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20251518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:05:24  来源:igfitidea点击:

Uncaught TypeError: String is not a function

javascripthtmlcss

提问by StudentwebCoding

I am new to JavaScript and as part of my project for college I need to include some. I created a simple .js file and linked it to my index.html file using <script src="main.js"></script>

我是 JavaScript 的新手,作为我大学项目的一部分,我需要包括一些。我创建了一个简单的 .js 文件并使用它链接到我的 index.html 文件<script src="main.js"></script>

I created another page called sellYourHome.htmland added a <section>tag with a table and created a simple form that calculates a comission and displays the results back in the input field.

我创建了另一个名为的页面sellYourHome.html并添加了一个<section>带有表格的标签,并创建了一个简单的表格来计算佣金并将结果显示在输入字段中。

function comCalc()
{
 prcHouse = parseFloat(document.getElementById('prcHouse').value);
 commision =  0.25; 
 result = prcHouse * commision;
 document.getElementById('prcHouse').value = result.toString();
}

That worked.

那行得通。

Now under that function, I wanted to create another one that picks up a <div>element and changes the background-colorto, say, blue.

现在在该函数下,我想创建另一个函数来拾取一个<div>元素并将其更改为background-color蓝色。

So I created a form on index.htmland linked the same .js file to it in the same way.

所以我创建了一个表单index.html并以相同的方式将相同的 .js 文件链接到它。

This is my HTML:

这是我的 HTML:

<div id="mainContent">
    <p>hello</p>
    <form id="bgColor">
        <table>
            <tr>
                <td>
                    <input type="button" value="blue" onclick="bgColor('blue');">
                </td>
            </tr>
        </table>
    </form><!-- end of bgColor Form-->
</div>

My javascript is like so:

我的 javascript 是这样的:

function comCalc()
{
    prcHouse = parseFloat(document.getElementById('prcHouse').value);
    commision =  0.25;  
    result = prcHouse * commision;
    document.getElementById('prcHouse').value = result.toString();
}

function bgColor(color)
{
  var div = document.getElementById('mainContent');
  div.style.background=color;  
}

When I run this, I get this error on the console:

当我运行它时,我在控制台上收到此错误:

Uncaught TypeError: String is not a function

未捕获的类型错误:字符串不是函数

I am running this in a browser called SWRIron (which is based on Chrome and is HTML5-complient).

我在一个名为 SWRIron 的浏览器中运行它(它基于 Chrome 并且是 HTML5 兼容的)。

What am I doing wrong?

我究竟做错了什么?

回答by The Alpha

It's because of your function name bgColor, because bgcoloris an attribute of HTMLelement and it's clashing with your function bgcolor, browser treats this as an attribute(string) but you used it as a function, so it says string is not a function. So, change the function name to anything else like this

这是因为您的函数名称bgColor,因为它bgcolorHTML元素的属性,并且与您的函数发生冲突bgcolor,浏览器将其视为attribute(字符串),但您将其用作函数,所以它说string is not a function。因此,将函数名称更改为其他类似的名称

function setBgColor(color)
{
    var div = document.getElementById('mainContent');
    div.style.background = color;
}

Use it as

将其用作

<input type="button" value="blue" onclick="setBgColor('blue');" />

Example.

例子。

回答by Jacob

Yes. Since bgColor is system reserved for HMTL, it throws an error called "Uncaught TypeError: String is not a function". if you change the method name, it will work fine and works fine.

是的。由于 bgColor 是系统为 HMTL 保留的,它会抛出一个错误,称为“未捕获的类型错误:字符串不是函数”。如果您更改方法名称,它将正常工作并且工作正常。