我最初有这个代码在一个项目上工作但是当我决定将它移植到另一个项目时,它与$
有问题。
所以我决定使用jQuery.noConflict()
方法来解决它。它解决得很好,但.attr()
方法现在返回undefined。
初始代码
$(".sharebtn").click(function(event)
{
event.preventDefault();
content = $(this).attr("data-share-content"); content_id = $(this).attr("data-share-contentid"); medium=$(this).attr("data-share-medium");
cur_count = parseInt($("#share_count_holder_"+content_id).val());
if(cur_count<=999){$("#post-share-count").html((cur_count+1));}
if(cur_count>999 && cur_count<=1000000){ disp=parseFloat(Math.round((cur_count/1000)+'e1')+'e-1'); $("#post-share-count").html(disp+"K"); }
if(cur_count>1000000){ disp=parseFloat(Math.round((cur_count/1000000)+'e1')+'e-1'); $("#post-share-count").html(disp+"M"); }
$("#share_count_holder").val((cur_count+1));
window.open($(this).attr("data-share-link"),"popupWindow","width=600,height=400,scrollbar=yes");
var url = bh_url+'/admin-2/modules/blog/candor_blogger_ajax.php?action=run_share';
$.post(url,{ content_type:content, content_id:content_id, medium:medium} ,function(data) { },"json");
});
noConflict()
之后
var bh = jQuery.noConflict();
bh(".sharebtn").click(function(event)
{
event.preventDefault();
content = bh(this).attr("data-share-content"); content_id = bh(this).attr("data-share-contentid"); medium=bh(this).attr("data-share-medium");
cur_count = parseInt(bh("#share_count_holder_"+content_id).val());
if(cur_count<=999){bh("#post-share-count").html((cur_count+1));}
if(cur_count>999 && cur_count<=1000000){ disp=parseFloat(Math.round((cur_count/1000)+'e1')+'e-1'); bh("#post-share-count").html(disp+"K"); }
if(cur_count>1000000){ disp=parseFloat(Math.round((cur_count/1000000)+'e1')+'e-1'); bh("#post-share-count").html(disp+"M"); }
bh("#share_count_holder").val((cur_count+1));
window.open(bh(this).attr("data-share-link"),"popupWindow","width=600,height=400,scrollbar=yes");
var url = bh_url+'/admin-2/modules/blog/candor_blogger_ajax.php?action=run_share';
bh.post(url,{ content_type:content, content_id:content_id, medium:medium} ,function(data) { },"json");
});
点击事件触发,但是当我将content
记录到控制台时,我得到了未定义。 - 当我恢复$
时,它在旧项目中工作正常。
可能是我的问题。
答案 0 :(得分:1)
尝试使用IFFE并传递jQuery,如下所示:
jQuery.noConflict(); // releases $ back to any other library that might be using it
(function($) { // IIFE passing in jQuery as $, inside the scope of this function $ is an now alias for jQuery
$(".sharebtn").click(function(event) {
event.preventDefault();
content = $(this).attr("data-share-content");
content_id = $(this).attr("data-share-contentid");
medium = $(this).attr("data-share-medium");
cur_count = parseInt($("#share_count_holder_" + content_id).val());
if (cur_count <= 999) {
$("#post-share-count").html((cur_count + 1));
}
if (cur_count > 999 && cur_count <= 1000000) {
disp = parseFloat(Math.round((cur_count / 1000) + 'e1') + 'e-1');
$("#post-share-count").html(disp + "K");
}
if (cur_count > 1000000) {
disp = parseFloat(Math.round((cur_count / 1000000) + 'e1') + 'e-1');
$("#post-share-count").html(disp + "M");
}
$("#share_count_holder").val((cur_count + 1));
window.open(bh(this).attr("data-share-link"), "popupWindow", "width=600,height=400,scrollbar=yes");
var url = bh_url + '/admin-2/modules/blog/candor_blogger_ajax.php?action=run_share';
$.post(url, {
content_type: content,
content_id: content_id,
medium: medium
}, function(data) {}, "json");
});
}(jQuery)); // pass in jQuery