简体   繁体   中英

Long Press - ASP.NET

I am in the process of handling a Long Press event in the JavaScript of an ASPX page but since I don't have much experience with JavaScript having a couple issues. I am working of a question which was already asked here .

When I run the code I get the message "$ is not defined" and when I change $("Button1") to ("Button1") then I get the message stating the mouseup function doesn't exist. The primary problem I'm having is accessing the aspx control properly. Below is my code.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" language="javascript">
        $(function () {
            var pressTimer;
            var longPress = 1000;

            $("#<%= Label1.ClientID %>").bind("touchend", function (e) {
                var d = new Date();
                var timeDiff = d - pressTimer
                if (timeDiff > longPress) {
                    document.getElementById("demo").innerHTML = "Mouse Up";
                    //actual logic here
                }
                return false;
            });
            $("#<%= Label1.ClientID %>").bind("touchstart", function (e) {
                pressTimer = new Date();
                return false;
            });
        });
    </script>
    <title>Long Press Testing</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:label ID="Label1" runat="server" text="Hold This Down" />
        <br />
        <p id="demo"></p>
    </div>
    </form>
</body>
</html>

Thanks for the help.

[EDIT] - Turns out I was missing the ready statement so the event is now firing as it should. This is the final version which is behaving properly. Also I wrote this to handle long press functionality on the iPad so if anyone is trying to do that this code is a good place to start.

You are missing jQuery script registration in head section like:

<script src="jquery.js"></script>

$ sign is jQuery's identifier, not JavaScript's.

Also, your buttons will not work, because you are referencing server button, but you must provide an id for JavaScript to work properly:

$("#<%= Button1.ClientID %>").mouseup(function () {
        clearTimeout(pressTimer)
        // Clear timeout
        return false;
    })

Looks like you are attempting to use jQuery (or another similar framework) without actually registering the appropriate script. You will need an <script/> block or some similar construct.

Looks like you havent included jquery in your project. you can include it by using the google hosting below, or go to jquery.com and download it and include it. your going to want to put in the Head tag above the other scripts you wrote. also, you might want to add a # before the id because it uses css selectors. Also, im not sure how much of the code will execute because thats a server button which will cause a post back.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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