首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(这).attr()在jquery.noConflict()之后停止工作

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

Stack Overflow用户
提问于 2015-03-28 22:48:50
回答 1查看 68关注 0票数 0

我的代码最初用于一个项目,但当我决定将它移植到另一个项目时,它与$有问题。所以我决定使用jQuery.noConflict()方法来解决这个问题。它解决得很好,但是.attr()方法现在返回未定义的。

初始码

代码语言:javascript
复制
    $(".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()

代码语言:javascript
复制
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");

});

click事件会触发,但是当我将content记录到控制台时,就会出现未定义。-当我恢复$时,它在旧项目中工作得很好。我有什么问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-28 23:03:42

尝试使用IFFE并传入jQuery,如下所示:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29323530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档