我对jQuery / javascript有点新鲜,以及它如何与HTML交互。我编写了代码,每当我的循环找到数组之间的匹配时,就会用类.data打印出一个新的div(不包括在代码中,我认为它已经足够长了)。但是,当我尝试使用jQuery切换.data div的类时,没有任何反应。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type='text/css' rel='stylesheet' href='NewCoBreakdown.css'/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$('.data').click(function() {
$(this).toggleClass('red'); //here i try to give the .data classes printed with
}); // JS the class .red
$('#remove').click(function(){
$('.red').remove(); ///and try to eliminate them
});
});
</script>
</head>
<body>
<div id="selection">
<select id="major">
<option value='false'> </option>
<option value="first">first</option>
<option value="second">second</option>
<option value="third">third</option>
</select>
<input type="button" value="go" onclick="execu();">
<div id='bresult'> </div>
<script>
function execu() {
var input = document.getElementById('major');
var choice = input.value;
switch(choice) {
case 'first':
for(var i=0; i<needmeche.length; i++) {
var need = needmeche[i];
for(var n=0; n<offarray.length; n++) {
var off = offarray[n];
if(need.name === off.name) {
var divTag = document.createElement('div');
divTag.className = 'data';
divTag.innerHTML = need.name + " - " + need.title + "text" +
need.description + "text" + need.profs;
document.getElementById('bresult').appendChild(divTag);
}
}
}
</script>
<div id="remove">Remove</div>
</div>
</body>
</html>
真的很感激任何帮助。谢谢!