我正在尝试调试,并且我一直收到语法错误。我不确定这是怎么回事。我想把它放在哪里,如果我点击附加到“$ color_cell”的对象,它会将atrr(“id”)推入black_colors数组。
有人可以帮我吗?提前谢谢。
<script src="jquery-1.6.2.js"></script>
<script type="text/javascript">
var ids = [];
var black_colors = [];
var blue_colors = [];
var brown_colors = [];
var gray_colors = [];
var green_colors = [];
var orange_colors = [];
var pink_colors = [];
var purple_colors = [];
var red_colors = [];
var teal_colors = [];
var white_colors = [];
var yellow_colors = [];
$(document).ready(function($) {
$(".btnColor").click(function(){
$(".color_cell").click(function(){
if $(this).attr("id") == "black"{
black_colors.push($(this).attr("id"));
}
});
});
});
</script>
<br>
<br>
<button type="button" class="btnColor" id="black">Black</button>
<button type="button" class="btnColor" id="blue">Blue</button>
<button type="button" class="btnColor" id="brown">Brown</button>
<button type="button" class="btnColor" id="gray">Gray</button>
<button type="button" class="btnColor" id="green">Green</button>
<button type="button" class="btnColor" id="orange">Orange</button>
<button type="button" class="btnColor" id="pink">Pink</button>
<button type="button" class="btnColor" id="purple">Purple</button>
<button type="button" class="btnColor" id="red">Red</button>
<button type="button" class="btnColor" id="teal">Teal</button>
<button type="button" class="btnColor" id="white">White</button>
<button type="button" class="btnColor" id="yellow">Yellow</button>
答案 0 :(得分:3)
而不是:if $(this).attr(“id”)==“black”
if ($(this).attr("id") == "black")
答案 1 :(得分:1)
我认为你的javascript语法不对。试试这个:
$(document).ready(function($) {
$(".btnColor").click(function(){
$(".color_cell").click(function(){
if ($(this).attr("id") == "black") {
black_colors.push($(this).attr("id"));
}
});
});
});
答案 2 :(得分:1)
您的代码中存在许多错误。
<script src="jquery-1.6.2.js"></script>
应该成为:
<script type="text/javascript" src="jquery-1.6.2.js"></script>
和
$(document).ready(function($) {
应该成为:
$(document).ready(function() {
和
if $(this).attr("id") == "black"{
应该成为:
if ($(this).attr("id") == "black") {
而且你还嵌套了两个不正确的.click()
处理程序。
所以最后你有:
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript">
var ids = [];
var black_colors = [];
var blue_colors = [];
var brown_colors = [];
var gray_colors = [];
var green_colors = [];
var orange_colors = [];
var pink_colors = [];
var purple_colors = [];
var red_colors = [];
var teal_colors = [];
var white_colors = [];
var yellow_colors = [];
$(document).ready(function() {
$(".color_cell").click(function() {
// Can't see any color_cell class in your markup
// so no idea what you want to do if the DOM element with it is clicked
});
$(".btnColor").click(function() {
if ($(this).attr("id") == "black") {
black_colors.push($(this).attr("id"));
}
});
});
</script>