最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Call javascript function from html - Stack Overflow

programmeradmin0浏览0评论

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

<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>

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

<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>
Share Improve this question edited Feb 6, 2013 at 3:16 JosephG asked Feb 5, 2013 at 5:58 JosephGJosephG 3,2537 gold badges35 silver badges57 bronze badges 2
  • 1 you might want to go through some basics of how HTML, Javascript etc work. – KBN Commented Feb 5, 2013 at 6:02
  • Is this how your javascript is embedded? – whastupduck Commented Feb 5, 2013 at 6:02
Add a ment  | 

1 Answer 1

Reset to default 1

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

<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>
发布评论

评论列表(0)

  1. 暂无评论