我在页面的标题部分使用 2 个 anchor 标记,我想捕获它们上的点击事件,以便我可以打开面板。如果我使用按钮而不是 anchor 标记,我可以使用它们的 id 轻松捕获它们,例如;
$(document).on('click', function(evt){
var target = evt.target;
var targetId = target.getAttribute('id');
if(targetId === 'openRightPanel'){
$('.right-panel').panel('open');
evt.stopImmediatePropagation();
evt.preventDefault();
}
}
但是如果我使用 anchor 标记,我将无法获得这样的点击事件;
if(targetId === 'openRightPanel'){
$('.right-panel').panel('open');
evt.stopImmediatePropagation();
evt.preventDefault();
}
即使我的 anchor 标记的 ID 为 openRightPanel。
我意识到 jQuery mobile 正在我的 html 中创建一些新的 div,因此我开始使用此代码来捕获该单击事件;
while (target !== null) {
if(target['id'] === 'openLeftPanel'){
$('.left-panel').panel('open');
isPreventOn = true;
}
else if(target['id'] === 'openRightPanel'){
$('.right-panel').panel('open');
isPreventOn = true;
}
target = target.parentNode;
}
这个工作正常,但在 android 4.1 上产生了一些问题,并且有一个 while 循环来捕获点击事件对我来说似乎不合适。
所以我的问题是如何在没有循环的情况下捕获对 jQuery mobile 中这些 anchor 标记进行的点击事件,或者是否有其他方法来获取该事件?这些是我想要获得的 anchor 标记;
<div data-role="header">
<h1>title</h1>
<a id="openLeftPanel">open</a>
<a id="openRightPanel">open</a>
</div><!-- /header -->
最佳答案
您可以直接在选择器上获取点击事件
$("#openLeftPanel").off('click').on('click', function(evt){
});
$("#openRightPanel").off('click').on('click', function(evt){
});
关于javascript - 在 jQuery Mobile 上选择 anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17801275/