我在這裏有一個創建工具提示的函數,這些工具提示是在無限滾動的頁面上創建的(例如,一旦我們點擊頁面底部函數被再次調用),也許值得一提的是,我還使用jScrollPane作爲自定義滾動條,並通過我的工具提示功能同時重新初始化。使用jQuery從dom中刪除特定的數據
問題在於,每次函數重新初始化時,我的工具提示都會一次填充多個圖像,每重新執行一次無限滾動回調就會重複一次。
這是功能:
function tooltipsInit() {
//Setting the targeted elements variable
var appliesTo = '.products .products-grid li .product-image-visible a, .featured .products-grid li .product-image-visible a, .new .products-grid li .product-image-visible a, .popular .products-grid li .product-image-visible a';
jQuery(appliesTo).mouseenter(function() {
jQuery(this).children('p').addClass('active');
if((navigator.platform.indexOf("iPad") != -1) || (navigator.platform.indexOf("iPhone") != -1)) {
//disabling the tooltips on iPad/iPhone
} else {
jQuery('body .wrapper').append('<div class="hcontainer"><div class="image"></div><div class="title"></div></div>');
jQuery(this).children('img').clone().appendTo('.hcontainer .image');
title = jQuery(this).children('img').attr('alt');
jQuery('.hcontainer .title').append(title);
jQuery('.hcontainer').fadeIn(200);
function removeDuplicates() {
//THIS IS MY QUESTION RELATED TO
jQuery.removeData();
}
removeDuplicates();
}
});
jQuery(appliesTo).mousemove(function(e) {
offsetX = 235;
offsetY = 175;
windowHeight = jQuery(window).height();
windowWidth = jQuery(window).width();
ypos = 0;
xpos = 0;
if((e.pageY + 355) < windowHeight) {
ypos = e.pageY + offsetY;
} else {
ypos = e.pageY - (offsetY + 30);
}
if((e.pageX + 455) < windowWidth) {
xpos = e.pageX + offsetX;
} else {
xpos = e.pageX - (offsetX + 30);
}
jQuery(".hcontainer").css("top", (ypos) + "px").css("left", (xpos) + "px");
});
jQuery(appliesTo).mouseleave(function() {
jQuery(this).children('p').removeClass('active');
jQuery('.hcontainer').fadeOut(100).remove();
});
};
像這個我清除整個DOM數據(?右)和我在Chrome瀏覽器開發工具的一些錯誤。
Uncaught TypeError: Cannot read property 'nodeName' of undefined jquery-1.8.3.min.js:1719
jQuery.extend.acceptData jquery-1.8.3.min.js:1719
jQuery.extend.removeData jquery-1.8.3.min.js:1633
removeDuplicates custom-scripts.js:160
(anonymous function) custom-scripts.js:162
jQuery.event.special.(anonymous function).handle jquery-1.8.3.min.js:3344
jQuery.event.dispatch jquery-1.8.3.min.js:3058
elemData.handle.eventHandle
我能做些什麼來準確地選擇被添加到DOM數據,並只刪除?
,如:removeData(mytooltipfunctionsData)
而且還對同一主題,我是相當新的JavaScript/jQuery的,你將如何改善功能?
編輯
我傻......我把你寫什麼有定睛一看,我仍然運行它,將被調用函數中...然後我意識到你所做的不需要一個功能,因爲正如你所說,這是事件代表團:)。
謝謝,它工作!在無限滾動中沒有重複和沒有錯誤。
因爲我不知道你在哪裏初始化jscrollpane,你試圖刪除什麼數據,更新你的答案,所以我可以更新我的 – pocesar