从 html 调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14701074/
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-08-24 17:39:49  来源:igfitidea点击:

Call javascript function from html

javascripthtml

提问by User093203920

I need to call a javascript function from html and it doesn't seem to be working. I click on the images and nothing happens. The variable "brushVar" doesn't seem to be changing. blue.png Is a small blue button, and blueN.png is a small blue button in a different shade of blue, etc...Any help is greatly appreciated

我需要从 html 调用一个 javascript 函数,但它似乎不起作用。我点击图片,没有任何反应。变量“brushVar”似乎没有改变。blue.png 是一个蓝色的小按钮,而 blueN.png 是一个不同深浅蓝色的蓝色小按钮,等等......非常感谢任何帮助

<body>

<script>
var brushVar;


function blue() {
    brushVar = ("#000000");
    alert(brushVar);
}

function blueN() {
    brushVar = ("#000000");
    alert(brushVar);
}

function cyan() {
    brushVar = ("#000000");
    alert(brushVar);
}

function cyanN() {
    brushVar = ("#000000");
    alert(brushVar);
}

function green() {
    brushVar = ("#000000");
    alert(brushVar);
}

function greenN() {
    brushVar = ("#000000");
    alert(brushVar);
}

function purple() {
    brushVar = ("#000000");
    alert(brushVar);
}

function purpleN() {
    brushVar = ("#000000");
    alert(brushVar);
}

function red() {
    brushVar = ("000000");
    alert(brushVar);
}

function redN() {
    brushVar = ("000000");
    alert(brushVar);
}

function yellow() {
    brushVar = ("#000000");
    alert(brushVar);
}

function yellowN() {
    brushVar = ("#000000");
    alert(brushVar);
}
</script>





<div id="buttons" style="position:absolute; width:100%; height:100%>
 <a href="#" onclick="blue()';" >
    <img src="/blue.png"/>
</a>
<a href="#" onclick="blueN()';" >
    <img src="blueN.png"/>
</a>
<a href="#" onclick="cyan()';" >
    <img src="cyan.png"/>
</a>
<a href="#" onclick="cyanN()';" >
    <img src="cyanN.png"/>
</a>
<a href="#" onclick="green()';" >
    <img src="green.png"/>
</a><a href="#" onclick="greenN()';" >
    <img src="greenN.png"/>
</a>
<a href="#" onclick="purple()';" >
    <img src="purple.png"/>
</a>
<a href="#" onclick="purpleN()';" >
    <img src="purpleN.png"/>
</a>
<a href="#" onclick="red()';" >
    <img src="red.png"/>
</a>
<a href="#" onclick="redN()';" >
    <img src="redN.png"/>
</a>
<a href="#" onclick="yellow()';" >
    <img src="yellow.png"/>
</a>
<a href="#" onclick="yellowN()';" >
    <img src="yellowN.png"/>
</a>
</div>
</body>

采纳答案by Kim D.

Your javascript must be enclosed by a script tag and your strings must be in quotes:

您的 javascript 必须用脚本标记括起来,并且您的字符串必须用引号引起来:

<div id="over" style="position:absolute; width:100%; height:100%>
 <img src="blue.png" href='' onclick='blue();'>
 <img src="blue.png"  href='' onclick='blue();' >
 <img src="blueN.png"  href='' onclick='blueN();' >
 <img src="cyan.png" href='' onclick='cyan();' >
 <img src="cyanN.png" href='' onclick='cyanN();'>
 <img src="green.png" href='' onclick='green();' >
 <img src="greenN.png" href='' onclick='greenN();'>
 <img src="purple.png" href='' onclick='purple();'>
 <img src="purpleN.png" href='' onclick='purpleN();'>
 <img src="red.png" href='' onclick='red();'>
 <img src="redN.png" href='' onclick='redN();'>
 <img src="yellow.png" href='' onclick='yellow();'>
 <img src="yellowN.png" href='' onclick='yellowN();'>
</div>

<script type="text/javascript">
    var color;

    function blue() {
        color = "blue";
    }

    function redN() {
        color = "red-N";
    }
</script>