簡體   English   中英

(this).attr()在jquery.noConflict()之后停止工作

[英](this).attr() stops working after jquery.noConflict()

我最初有在一個項目上工作的這段代碼,但是當我決定將其移植到另一個項目時,它與$產生了問題。 因此,我決定使用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記錄到控制台時,我未定義。 -當我恢復$ ,它在舊項目中可以正常工作。 可能是我的問題。

嘗試使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM