繁体   English   中英

检测特定元素的位置

[英]Detect location of certain element

假设我连续有三个按钮,最后一个是“致命”按钮。 有没有一种方法可以单击按钮1,它会告诉我距“致命”按钮还有2个元素? 或单击按钮2,它会告诉我它与数字3的“死”按钮相距一次。

我不确定我已经很好地解释了这一点,所以我将链接一个安装示例。

我希望做一些类似于扫雷的检测“地雷”的方法。

HTML

<table>
    <tr>
        <td><button id="1">Button 1</button></td>
        <td><button id="2">Button 2</button></td>
        <td><button id="3">Button 3</button></td>
    </tr>
    <tr>
        <td><button id="4">Button 4</button></td>
        <td><button id="deadly">Button 5</button></td>
        <td><button id="6">Button 6</button></td>
    </tr>  
    <tr>
        <td><button id="7">Button 7</button></td>
        <td><button id="8">Button 8</button></td>
        <td><button id="9">Button 9</button></td>
    </tr> 
</table>  

我目前正在使用JQuery,因此使用它的任何解决方案仍然对我有用。

小提琴

最简单的说,您可以使用<td>元素的cellIndex属性,其中包含<button>

$('button').click(function(e){
    e.preventDefault();
    var self = $(this),
        cellIndex = self.closest('td').prop('cellIndex'),
        lastCell = self.closest('tr').find('td:last-child').prop('cellIndex'),
        delta = Math.abs(lastCell - cellIndex);
    console.log( delta + (delta === 1 ? ' cell' : ' cells') + ' away from deadly cell');
});

JS小提琴演示

参考文献:

使用jQuery:

$("button").click(function() {
    var myrow = $(this).parent().index();
    var mycol = $(this).index();
    var deadly = $("#deadly");
    var deadly_row = deadly.parent().index();
    var deadly_col = deadly.index();
    var x_dist = Math.abs(mycol - deadly_col);
    var y_dist = Math.abs(myrow - deadly_row);
    // Do what you want with x_dist and y_dist
});

此代码随机确定致命的正方形,然后检查onclick。 首先,它检查坐标以查看它们是否匹配,如果不匹配,则确定距离。 在这里,我只是通过函数调用传递坐标。 您可以根据需要动态增加表格。 这种方法的额外好处是您不能仅查看源并找出哪个按钮“死”了。

<script>
    var DeadlyX = Math.floor((Math.random() * 3) + 1);
    var DeadlyY = Math.floor((Math.random() * 3) + 1);


    function amIDeadly(x, y) {

        var r = "checking";
        if ((x == DeadlyX) && (y == DeadlyY)) {
            r = "found the deadly!";
        }
        else {
            var dx = x - DeadlyX;
            var dy = y - DeadlyY;
            var hyp = dx*dx + dy*dy;
            hyp = Math.sqrt(hyp);
            hyp = Math.floor(hyp);
            r = "you are " + hyp + " from the deadly";
        }
        document.getElementById("infobox").value = r;

    }
</script>
<table>
    <tr>
        <td>
            <button id="1" onclick="amIDeadly(1,1);">Button 1</button>
        </td>
        <td>
            <button id="2" onclick="amIDeadly(1,2);">Button 2</button>
        </td>
        <td>
            <button id="3" onclick="amIDeadly(1,3);">Button 3</button>
        </td>
    </tr>
    <tr>
        <td>
            <button id="4" onclick="amIDeadly(2,1);">Button 4</button>
        </td>
        <td>
            <button id="5" onclick="amIDeadly(2,2);">Button 5</button>
        </td>
        <td>
            <button id="6" onclick="amIDeadly(2,3);">Button 6</button>
        </td>
    </tr>
    <tr>
        <td>
            <button id="7" onclick="amIDeadly(3,1);">Button 7</button>
        </td>
        <td>
            <button id="8" onclick="amIDeadly(3,2);">Button 8</button>
        </td>
        <td>
            <button id="9" onclick="amIDeadly(3,3);">Button 9</button>
        </td>
    </tr>
</table>
<br/>
<br/>
<input id="infobox" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM