本文介绍了如何获取触发taphold的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你能帮我找到哪个元素taphold"?是使用 JS、jQuery 还是 jQuery Mobile 触发的?
我的 HTML 结构如下
$(document).on("pagecreate", function () {$("#myFilesListView").bind('contextmenu', function (event) {event.preventDefault();event.stopPropagation();返回假;});});$(document).ready(function () {$("#myFilesListView").bind("taphold", function (event) {event.preventDefault(false);event.stopPropagation();var ID = $(this).child().attr("id");警报(ID);});});</script>
<div data-role="header"></div><div data-role="main"><ul data-role="listview" id="mylistview">
一些内容一些内容一些内容一些内容一些内容<!--id 不在预定义的序列中,可能有 100 个列表--><div data-role="fotter"></div>
在我的 JavaScript 代码中,我能够阻止 taphold 的默认行为,但是我不知道如何在用户点击并按住该列表时立即获取该列表的 ID.
解决方案
你可以将taphold绑定到li元素而不是listview:
$(document).on("pagecreate", "#page1", function () {$("#mylistview").on('contextmenu', function (event) {event.preventDefault();event.stopPropagation();返回假;});$("#mylistview li").on("taphold", function (event) {var ID = $(this).prop(id");警报(ID);});});
演示
Can you please help me to locate on which element "taphold" is fired by using JS, jQuery, or jQuery Mobile?
My HTML structure is like the 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.
解决方案
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
这篇关于如何获取触发taphold的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!