Javascript JQuery列表,防止链接被多次单击
我一直想弄明白这一点已经有一段时间了。我有一个使用JSon数组填充的JQuery列表 列表中的每个项目都是可单击的,并会显示一个名为“详细信息”的页面,其中包含所单击项目的特定详细信息 一切正常,但如果用户在一个项目链接上单击几次,页面将正确加载,但需要单击几次后退按钮才能返回到原始页面。假设用户点击一个项目3次,当他想返回时,他必须回击3次 所以我正在寻找一种方法,在点击一次后禁用链接 我怎样才能做到呢 下面的大代码示例,如果我的问题不清楚,请告诉我。谢谢Javascript JQuery列表,防止链接被多次单击,javascript,jquery,json,Javascript,Jquery,Json,我一直想弄明白这一点已经有一段时间了。我有一个使用JSon数组填充的JQuery列表 列表中的每个项目都是可单击的,并会显示一个名为“详细信息”的页面,其中包含所单击项目的特定详细信息 一切正常,但如果用户在一个项目链接上单击几次,页面将正确加载,但需要单击几次后退按钮才能返回到原始页面。假设用户点击一个项目3次,当他想返回时,他必须回击3次 所以我正在寻找一种方法,在点击一次后禁用链接 我怎样才能做到呢 下面的大代码示例,如果我的问题不清楚,请告诉我。谢谢 var items = [];
var items = [];
$.each(catalog.products,
function(index, value) {
if (
((!filterValue ) || value.name.toUpperCase().indexOf(filterValue.toUpperCase()) != -1)
&& ((!brand) || value.brand.toUpperCase().indexOf(brand.toUpperCase()) != -1)
&& ((!category) || value.category.toUpperCase().indexOf(category.toUpperCase()) != -1)
&& ((!sport) || value.sport.toUpperCase().indexOf(sport.toUpperCase()) != -1)
) {
var priceInfo;
if(value.salePrice === '') {
priceInfo = '<h4 style="margin-top:3px;margin-bottom:0px;color:#75a8db "> $' + value.price + '</h4></a></li>';
} else {
priceInfo = '<h4 style="margin-top:3px;margin-bottom:0px; "><span style="text-decoration: line-through;font-size:small;">$' + value.price +
'</span><span style="color:#75a8db;"> $' + value.salePrice + '</span></h4></a></li>';
}
items.push('<li id="' + index + '">' +
'<a data-identity="productId" href="./details.page?productId=' + index + '" >' +
'<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' +
'<p style="margin-bottom:0px;margin-top:0px;">' + value.brand + '</p>' +
'<h3 style="margin-top:0px;margin-bottom:0px;">' + value.name + '</h3>' +
priceInfo);
}}
);
if (items.length === 0) {
items.push('<p style="text-align: left;margin-left: 10px">No results found.</p>');
}
productListView.html(items.join(''));
productListView.listview('refresh');
var项目=[];
美元/件(目录.产品、,
函数(索引、值){
如果(
(!filterValue)| value.name.toUpperCase().indexOf(filterValue.toUpperCase())!=-1)
&&(!brand)| value.brand.toUpperCase().indexOf(brand.toUpperCase())!=-1)
&&((!category)| value.category.toUpperCase().indexOf(category.toUpperCase())!=-1)
&&((!sport)| value.sport.toUpperCase().indexOf(sport.toUpperCase())!=-1)
) {
var-priceInfo;
如果(value.salePrice==''){
价格信息=“$”+value.price+”;
}否则{
priceInfo='$'+value.price+
“$”+value.salePrice+”;
}
items.push(“”+
'' +
'' +
“”+value.brand+“
”+
“”+value.name+“”+
价格信息);
}}
);
如果(items.length==0){
items.push(“未找到结果。
”;
}
html(items.join(“”));
productListView.listview(“刷新”);
}您可以使用jQuery的
one()
实现此一次性操作,例如:
$('a').one('click',
function(){
alert('hi!');
return false;
});
或者,不使用one()
,只需从元素中解除click()
事件的绑定即可:
$('a').click(
function(){
alert('hi!');
$(this).unbind('click');
return false;
});
。
参考资料:
// here's the click handler you need
itemLink.click(function(e) {
console.log("item clicked");
if ($(this).data("clickCount") > 0) {
console.log("click count reached");
return false;
}
console.log("following the link");
// set a click counter
$(this).data("clickCount", 1);
// store the original href in case you need it
$(this).data("ogHref", $(this).attr("href"));
$(this).attr("href", "javascript://");
});
在fiddle中,我已经尽可能多地使用了您的代码,但是使用jQuery对象创建了标记
var currentValue="";
$('a').click(function(event) {
event.preventDefault();
if(currentValue != $(this).attr("href")){
currentValue = $(this).attr("href");
// Your actions here
}
});
因此,当链接的操作是最新的时,链接就消失了,但是可以重用 谢谢你的帮助。我想我明白了你想展示给我的东西,但我不确定如何在我的代码中实现这一点。还有什么线索吗?因为我尝试的东西不起作用。考虑到这是我想在单击后禁用的链接。“”+当我在我的html页面中放置一个链接时,这会起作用,但当我尝试将此链接附加到javascript文件中的“”链接时,它就不起作用了。请尝试$('a').live('click',function(event){…});还是没什么。无论我把代码放在哪里,它总是把我的列表弄乱,并返回我的错误消息“找不到结果”,而列表是空的。真的很感谢你的帮助。非常感谢。所以现在它似乎在我页面上的每个链接上都起作用,但在这个“我做错了什么?嘿,我只想说。我对您的代码做了一些修改,并设法使其适合我的情况。我真的很感谢你的帮助和努力,所以非常感谢你!干杯