(this).attr() 在 jquery.noConflict() 后停止工作
(this).attr() stops working after jquery.noConflict()
我的这段代码最初适用于一个项目,但当我决定将其移植到另一个项目时,$
出现了问题。
所以我决定使用jQuery.noConflict()
的方法来解决。它解决得很好,但是 .attr()
方法现在 returns 未定义。
初始代码
$(".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
记录到控制台时,我得到了未定义的信息。 - 当我恢复 $
时,它在旧项目中工作正常。
可能是我的问题。
尝试使用 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
我的这段代码最初适用于一个项目,但当我决定将其移植到另一个项目时,$
出现了问题。
所以我决定使用jQuery.noConflict()
的方法来解决。它解决得很好,但是 .attr()
方法现在 returns 未定义。
初始代码
$(".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
记录到控制台时,我得到了未定义的信息。 - 当我恢复 $
时,它在旧项目中工作正常。
可能是我的问题。
尝试使用 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