感谢您的帮助。它在jsfiddle / jsbin(翻转和退出)中都可以很好地工作,但是我似乎无法使它真正在浏览器中运行。

JavaScript-在您的帮助下进行了一些编辑,以匹配我学习过Math.random等的方式

colors = ['red','blue','green','gray','black','yellow'];
$(".test a").hover(function() {
  randomColor = Math.floor(Math.random() * colors.length - 1)
  $(this).css('color',colors[randomColor])
    }, function() {
      $(this).css('color', '#000000')
});


HTML(实际上使用了不同的html,但这是从jsbin中拉出的,它不起作用)

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="randomcolor.js"></script>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup,
  menu, nav, section { display: block; }
</style>
</head>
<body>
    <div class="test"><a href="http://test.com">WORKKKKK</a></div>
</body>
</html>


和浏览器代码的唯一区别是我在randomcolor.js文件中有js,在标头中使用

<script type="text/javascript" src="randomcolor.js"></script>


根据我在网上找到的信息,这与“加载”有关吗?我该如何实施?我也尝试过将此脚本直接放在html IN主体中,但仍然无法正常工作。

但是再次感谢你们最初的帮助,让它在jsfiddle中正常工作绝对是一个好的开始!

也只是为了提供尽可能多的信息-尝试添加此onload仍然无法正常工作

$(window).load(function(){
    colors = ['#ffffff', 'blue', 'green', 'gray', 'black', 'yellow'];
    $(".test a").hover(function() {
        randomColor = Math.floor(Math.random() * colors.length - 1)
        $(this).css('color', colors[randomColor])
    }, function() {
        $(this).css('color', '#000000')
    }
});

最佳答案

您应该使用$("div.input")而不是$("div input")
(请参见中间的点)

07-28 04:57