how to get the element on which taphold is fired(jquery mobile)

三世轮回 提交于 2019-12-12 03:18:18

问题


Can you please help me to locate on which element "taphold" is fired by using js, jquery or jq mobile. my html structure is like mentioned below

<script>
    $(document).on("pagecreate", function () {      
        $("#myFilesListView").bind('contextmenu', function (event) {
            event.preventDefault();
            event.stopPropagation();
            return false;
        });
    });
    $(document).ready(function () {
        $("#myFilesListView").bind("taphold", function (event) {
            event.preventDefault(false);
            event.stopPropagation();           
            var ID = $(this).child().attr("id");
            alert(ID);
        });
    });
</script>
    <div data-role="page" id="page1">
        <div data-role="header"></div>
        <div data-role="main">
            <ul data-role="listview" id="mylistview">
                <li class="mydata" id="1"> some conetent</li>
                <li class="mydata" id="2"> some conetent</li>
                <li class="mydata" id="3"> some conetent</li>
                <li class="mydata" id="4"> some conetent</li>
                <li class="mydata" id="5"> some conetent</li>
              <!--ids are not in predefined sequences and there may be 100s of list--> 
            </ul>
        </div>
 <div data-role="fotter"></div>
</div>
in my javascript code I am able to prevent the default behavior of taphold, but I am not getting how to get the Id of a particular list as soon as a user tap and hold on that list.

回答1:


You can bind the taphold to the li elements instead of the listview:

$(document).on("pagecreate", "#page1", function () {      
    $("#mylistview").on('contextmenu', function (event) {
        event.preventDefault();
        event.stopPropagation();
        return false;
    });

    $("#mylistview li").on("taphold", function (event) {
        var ID = $(this).prop("id");
        alert(ID);
    });
});

DEMO



来源:https://stackoverflow.com/questions/32902572/how-to-get-the-element-on-which-taphold-is-firedjquery-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!