简体   繁体   English

更改后台javascript功能不起作用

[英]Change Background javascript function not working

The code (below) seems to compile without any errors, but the "changebackground" function isn't working. 代码(下面)似乎可以正确编译,但是“ changebackground”功能无法正常工作。 it does nothing when you click on it. 单击它时不执行任何操作。 I don't think there is a problem with Syntax, but cant be sure. 我认为语法没有问题,但是不能确定。 There are no errors, just no response when i click on the cell. 单击单元格时没有错误,只有响应。 "MyClick" works, bu t"ChangeBackground" doesn't. “ MyClick”有效,但“ ChangeBackground”无效。

Any thoughts?? 有什么想法吗??

<html><body>
<head>
<style>
table,th
{ 
border:1px solid black
font-size:  15px;
font-family: Arial;
font-weight: bold;
empty-cells: show;
}
</style>
<style>
td
{ 
font-size: 10px;
font-weight: normal;
font-family: Arial;
border:1px solid black;
empty-cells: show;
align = "middle;"
}
</style>
<style>
p
{ 
font-size: 18px;
font-weight: bold;
font-family: Arial;
}
</style>
<style>
a.1{ text-decoration:none;color:WindowText}
</style>
<style>
#header{ display:block;top:0px;left:0px;width:100%;height: 112px;position:fixed;background-color: #ffffff;border:1px solid #888;}
</style>
<style>
#content{ margin:113px 0px 0px 0px;display:block;border:1px solid #888;}
</style>


<script type="text/javascript"> function myClick(args) {    window.clipboardData.setData('text',args.toString());   }</script>

<script type="text/javascript"> function changeBackground() {document.getElementById(cellID).style.borderColor = "2px solid red";   }</script>


</head>
 <p>  Scanned Samples </p> <table></table></div>  <p> Rack: 202771 - _SMEYER_IND_AC_2D-02 </p>

<table> 

<thead> 

<tr> 
 <font size = "10"> </font> 

<th> </th>

<th>1</th>

<th>2</th>

<th>3</th>

<th>4</th>

<th>5</th>

<th>6</th>

<th>7</th>


</tr> 

</thead> 

<td> <font size = "2"><b>A</b></td>

<td><a class = "1" href = "#abcd"onclick="javascript:myClick('202772')"><center>A1<br>0<br>(202772)</center></td>
 <td id = "Cell9"><a class = "1" href = "#abcd"onclick=" javascript:myClick('202780') "; javascript:changeBackground('Cell9')"> <center> A2<br>0<br>(202780)</center> </td>

<td id = "Cell17"><a class = "1" href = "#abcd"onclick=" javascript:myClick('202788') "; javascript:changeBackground('Cell17')"> <center> A3<br>0<br>(202788)</center> </td>


<td id = "Cell25"><a class = "1" href = "#abcd"onclick=" javascript:myClick('202796') "; javascript:changeBackground('Cell25')"> <center> A4<br>0<br>(202796)</center> </td>


<td id = "Cell33"><a class = "1" href = "#abcd"onclick=" javascript:myClick('202804') "; javascript:changeBackground('Cell33')"> <center> A5<br>0<br>(202804)</center> </td>


<td id = "Cell41"><a class = "1" href = "#abcd"onclick=" javascript:myClick('202812') "; javascript:changeBackground('Cell41')"> <center> A6<br>0<br>(202812)</center> </td>


<td id = "Cell49"><a class = "1" href = "#abcd"onclick=" javascript:myClick('202820') "; javascript:changeBackground('Cell49')"> <center> A7<br>0<br>(202820)</center> </td>


</tr>
 </table> 
 </body></html>

You're calling changeBackground with parameters when the function definition didn't have it. 当函数定义没有它时,您将使用参数调用changeBackground Here's an example 这是一个例子

javascript:changeBackground('Cell9')"

Here is your function definition 这是您的功能定义

function changeBackground() {
    document.getElementById(cellID).style.borderColor = "2px solid red";   
}

Try changing that to 尝试将其更改为

function changeBackground(cellID) {
    document.getElementById(cellID).style.borderColor = "2px solid red";   
}

Also, this is pretty bad form too. 同样,这也是很糟糕的形式。 You should prefer stylesheets over style tags, and you have a bunch of them. style标签相比,您应该更喜欢stylesheets ,并且有很多stylesheets Same thing with the script tags. script标签相同。 Please place them in an external JavaScript file. 请将它们放在外部JavaScript文件中。

Also, you are using inline javascript too often like this example 另外,您像本例一样经常使用inline javascript

onclick=" javascript:myClick('202812') "; javascript:changeBackground('Cell41')"

This makes the HTML really difficult to read. 这使得HTML确实很难阅读。 You should instead use event listeners and place them in your JavaScript file. 相反,您应该使用事件侦听器,并将它们放在JavaScript文件中。

Also, the center and font tags are not supported in HTML5. 此外,HTML5不支持centerfont标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM