2

我正在尝试使用 html5/jquery 开发简单的鲍鱼游戏,但我发现了我无法向自己解释的问题。

有一个板子,是jquery绘制的,还有一些可移动的球(也是jquery绘制的)

我想根据当前的 DOM 结构向服务器发送 ajax 请求。

我有这样的源代码(来自萤火虫):(它是带有一些 id 的瓷砖,包括一些图像,也许还有球)

<div class="tile ui-droppable" id="tile0-5" style="position: absolute; top: 364px; left: 0px; width: 72px; height: 72px;">
    <img src="/abalone/www/images/tile.png" width="72" height="72" alt="tile">
    <img class="ball white-ball ui-draggable" src="/abalone/www/images/white-ball.png" width="36" height="36" alt="white-ball" id="ball0-5" style="position: absolute; top: 18px; left: 18px; z-index: 99999;">
</div>

所以假设瓷砖的 id 是 tile0-5 我尝试写入控制台(但常规脚本都不起作用)

$("#tile0-5").children()

我期待这两个图像 - 瓷砖和球,但只有瓷砖被退回。我已经尝试添加选择器

$("#tile0-5").children(".ball")

返回空数组(我的意思是没有结果)

这是我面临的最后一个问题,有人可以帮我吗?我使用最新的 jquery 稳定版本

非常感谢,伙计们

编辑:我也试过

    $("#tile0-5").find(".ball")

这也没有用。

4

3 回答 3

0

这个也更有帮助!!!

$("#tile0-5 .ball")

输出包含在 tile0-5 Id Container 中具有球类的元素列表

于 2013-09-15T08:30:44.967 回答
0
<script>
jQuery(document).ready(function()
{
    $("#tile0-5 img").each(function()
    {
        if($(this).hasClass('ball'))
        {
            alert($(this).attr("src"));
        }   
    });
});
</script>

此功能可帮助您查找每个功能中具有“球”类的所有图像

于 2013-09-15T08:57:26.310 回答
0

需要上下文,谢谢帮助

function endTurnButtonClicked () {
$.log("Sending data to server...");
$($.endTurnButton).prop('disabled', true);

var data = {};
tiles = $(".tile");
$.each(tiles, function (key, tile) {
    //make 2D array
    if (! $.isArray(data)) data = Array();
    if (! $.isArray(data[$(tile).data("row")])) data[$(tile).data("row")] = Array();

    //var ball = $(tile).children(".ball"); //does not find any
    var tileId = $(tile).attr("id");
    var ball = $("#" + tileId + " .ball"); //this works, but it's so clumsy

    //---- rest of code (no problem here) ----
    var color = $.NONE; 

    if ($(ball).length == 1) {
         color = $(ball).data("color");
    }
    else if ($(ball).length > 1) {
        color = "ERROR";
    }
    data[$(tile).data("row")][$(tile).data("column")] = color;
});

$.ajax({
    url:    $.linkNextRound(),
    cache:  false,
    dataType:   'json',
    data:   data,
    type:   'POST',
    success:    function (data) {
        $.log("Success", "success");
        waitForNextRound();
    },
    error:  function (data) {
        $.log("Error while sending data to server.", "error");
        $($.endTurnButton).prop('disabled', false);
    }
}); 

}

于 2013-09-15T11:00:15.923 回答