鼠标单击无法提供坐标

时间:2016-10-01 09:29:23

标签: javascript jquery html

我想在点击事件中检索鼠标坐标并将其显示给我。目前它无法正常工作。

这是我的剧本:

public class Main {

    public static void main(String[] args) {
        String cipher = "10#21#12#91";

        System.out.print(decrypt(cipher));

        //output : julia
    }

    static String decrypt(String cipher) {
        //split with # to obtain array of code in string array
        String[] cipher_char_codes = cipher.split("#");

        //create empty message
        StringBuilder message = new StringBuilder();

        //loop for each code
        for (String code : cipher_char_codes) {

            //get index of character
            int index = Integer.parseInt(code);

            if (index > 26) {
                char[] pair = code.toCharArray();
                for (int i = 0; i < pair.length; i++) {
                    int x = Integer.parseInt("" + code.charAt(i));
                    message.append((char) ('a' + ((x - 1) % 26)));
                }
            } else {
                //map index into 1 to 26
                //find ascii code and cast into char
                message.append((char) ('a' + ((index - 1) % 26)));
            }
        }
        return message.toString();
    }

}

3 个答案:

答案 0 :(得分:3)

试试这个..

$(document).ready(function(){
   $("#image").click(function (e){
      x=e.pageX;
      alert(x);
   });
});
<!doctype html>
<html lang="en">
<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">    </script>
</head>
<body>
	<img src="http://ultraimg.com/images/2016/07/29/Simplest-Responsive-jQuery-Image-Lightbox-Plugin-simple-lightbox.jpg" id="image" height="100px" width="100px" />
</body>
</html>

答案 1 :(得分:1)

<强>被修改

速记功能对我来说不适用于本地OP,而功能块工作正常但在创建jsFiddle时功能正常jsFiddle Here

$(document).ready(function(){
    $("#image").click(function(e){
        x=e.pageX;
        alert(x);
    });
});

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<body>


<p><strong>Tip:</strong> Try to click different places on image</p>
<img src="http://i164.photobucket.com/albums/u8/hemi1hemi/COLOR/COL9-6.jpg"  width="100" height="100"   onclick="showCoords(event)"/>
<p id="demo"></p>

<script>
function showCoords(event) {
var x = event.clientX;
var y = event.clientY;
var coords = "X coords: " + x + ", Y coords: " + y;
document.getElementById("demo").innerHTML = coords;
}
</script>                
     </body>
        </html>