gpt4 book ai didi

javascript - 警报事件有效,但我的单击更改颜色的 javascript 函数不起作用

转载 作者:行者123 更新时间:2023-11-28 13:35:28 26 4
gpt4 key购买 nike

我在head标签中创建了一个名为toggleColor的javascript函数。这样,我想在单击时将表行颜色更改为红色。在我的 td 标签内的主代码底部,我为我的函数添加了单击功能,但由于某种原因,这不起作用。当我在单击中对单击某一行进行硬编码和警报功能时,它会打开警报,但是当我什至尝试通过将颜色更改为红色来在我的 td 标记中进行硬编码时,它仍然不起作用,这非常令人困惑。我为此研究了一些资源,并且完全按照他们设置的方式完成了操作,但仍然没有运气。非常感谢一些帮助。

主要代码:

<?php print( '<?xml version = "1.0" encoding = "utf-8"?>') ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>User selection page</title>
<link rel="stylesheet" href="gameViewStyleSheet.css" type="text/css" />

<script type="text/javascript">
function toggleColor(this)
{
this.style.backgroundColor='red';
}
</script>
</head>


<?php
/*
Version 1.1: Instead of printing the teams in different cells we are going to print the
games in one row and we select the game itself to see if an upset will occur. This version
provides this functionality with a table for each conference. Currently Data is still hard-
coded and each table prints the west conference data. We will pull data from the DB once it
is set up. That will be provided in the next version.
*/

require_once('Conference.php');

for ($j = 0; $j<4; $j++)
{
$loadGameClass = new Conference();
$loadGameClass->loadTeams(array("(1)Gonzaga vs (16)Southern U", "(8)Pittsburgh vs (9)Wichita St", "(5)Wisconsin vs (12)Ole Miss", "(4)Kansas st vs (13)Boise St", "(6)Arizona vs (11)Belmont", "(3)New Mexico vs (14) Harvard", "(7)Notre Dame vs (10)Iowa St", "(2)Ohio St vs (15) Iona"));
$teams = $loadGameClass->getTeams();

echo '<table border="1" align="center">';

switch ($j) {
case 0:
echo "Midwest";
break;
case 1:
echo "West";
break;
case 2:
echo "South";
break;
case 3:
echo "East";
break;
}

//echo '<div class = ".table_entries">';

for ($i = 0; $i < 8; $i++)
{
$games = $teams[$i];
echo '<tr><td onclick="toggleColor(this)">'.$games.'</td><tr>';
}

echo '</table>';
echo "<br>" . "<br>";

//echo '</div>';

}
?>

<body>
</body>
</html>

session :

<?php

class Conference
{
protected $teams;

function loadTeams($teams)
{
$this->teams = $teams;
}

function getTeams()
{
return $this->teams;
}
}

?>

最佳答案

您不能使用名为 this 的参数,因为 this 在 Javascript 中具有特殊含义。

更改以下功能:

function toggleColor(this)
{
this.style.backgroundColor='red';
}

致:

function toggleColor(el)
{
el.style.backgroundColor='red';
}

更新:

function toggleColor(el)
{
var s = el.style;
s.backgroundColor = (s.backgroundColor === 'red' ? 'white' : 'red');
}

关于javascript - 警报事件有效,但我的单击更改颜色的 javascript 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21740562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com