小齐齐齐齐齐齐 2016-04-19 06:51 采纳率: 27.8%
浏览 2119

JS,jQuery代码 封装到一个JS文件的问题

$(document).ready(function(){
$(".liren_content:odd").css("float",'right');
$('.mall_list').find('ul li:nth-child(3)').css('border','0');
var count=0;

  $(".jiaru").click(function () {
    // alert(111)
    count=parseInt($(".ico_img b").html());
    var id = $(this).next().html();
    //count++;
    $.ajax({
      url:"__MODULE__/Qiangbao/check",
      type:"POST",
      data:"id="+id,
      dataType:"text",
      async:true,
      success:function(res){
        //alert(count);
        if(res=="M"){
          //alert(count);
          var a = count +1;
          parseInt($(".ico_img b").html(a));
        }
      }
    });

    $(".ico_img b").html(count);

    var flyElm = $(this).parents(".liren_content").clone();
    flyElm.css({
      'opacity':'0.7',
      'z-index': 9000,
      'display': 'block',
      'position': 'absolute',
      'top': $(this).parents(".liren_content").offset().top +'px',
      'left': $(this).parents(".liren_content").offset().left +'px',
      'width': $(this).parents(".liren_content").width() +'px',
      'height': $(this).parents(".liren_content").height() +'px'
    }).addClass("remove");
    $('body').append(flyElm);
    flyElm.animate({
      top:$('#cart img').offset().top,
      left:$('#cart img').offset().left,
      width:20,
      height:20,
      opacity:0
    },'slow');
    setTimeout(function(){
      flyElm.remove();

    },1000)
    // var id = $(this).next().html();
    // var aaa = '<{session.shops.id}>';
    // if(in(id,aaa)){
    //   alert(id);
    // }
    //alert(id);
    //加入购物车操作
    $.ajax({
      url:"__MODULE__/Qiangbao/addlist",
      type:"POST",
      data:"id="+id,
      dataType:"text",
      async:true,
      success:function(res){
        //alert(res);
        if(res=="Y"){

        }
        if(res=="N"){
          alert('请再点击一次!');
        }
      }
    });
  })

$('.mall_title').click(function(){
window.location.href="__MODULE__/Qiangbao/Index";
})

$('.liren_title').click(function(){
window.location.href="__MODULE__/Liren/Index";
})

//返回顶部
$('.backtop').click(function (e) {
$(".gotop").css({"background-image":"url(__PUBLIC__/images/images/iconfont-fanhuidingbu_up.png)"});
e.preventDefault();
$('html,body').animate({ scrollTop:0});
});

});

var page = 2;
var num = 6;
$(window).scroll(function () {

var scrollHeight = $(document).height();// 获取页面的文档高度
var scrollTop = $(window).scrollTop();// 获取滚动条到顶部的垂直高度
var height = scrollHeight/2;
scrollTop > 375 ? $(".gotop").fadeIn(200).show() : $(".gotop").fadeOut(200).css({"background-image":"url(__PUBLIC__/images/images/iconfont-fanhuidingbu.png)"});
// $("#lb").text($(window).scrollTop()+' 滚动条的到顶部的距离');
//     $("#2b").text($(window).height()+' 文档的高度');
//     $("#3b").text($(document).height()+' 整个页面的高度');
if(scrollTop >= height){
  chaliren();
  page++;
}

});

function chaliren(){
$.post("__URL__/chaliren",{'page':page,'num':num},function(data){
var list = eval("("+data+")")
if(list.length==0){
page = 1;
}else{
str = '';
for (var i = 0; i < list.length; i++) {

            str += '<div class="liren_list"><a href="__MODULE__/Qiangbao/rewarddetail/id/'+list[i].id+'"><div class="liren_content"><img src="'+list[i].zhongpic+'">';
          if(list[i].zeroqiang > 0){str +='<img src="__PUBLIC__/images/images/Groupc 2@2x.png" class="lingyuanchou">';}else if(list[i].sala >0){str +='<img src="__PUBLIC__/images/images/Group@2x.png" class="jinritejia">';}
            str +='<nobr><h2>'+list[i].name+'</h2></nobr><div class="jindu">';
            if(list[i].zeroqiang >0){str +='<p style="margin-top:6px;">已参与&nbsp;<b>'+list[i].click+'</b></p><button class="button">立即参与</button><span style="display:none;">'+list[i].id+'</span>';}else{str +='<p>开奖进度<b>'+list[i].jindu+'%</b></p><a href="javascript:;" class="button jiaru">加入清单</a><span style="display:none;">'+list[i].id+'</span><span><i style="width:'+list[i].jindu+'%"></i></span>';}
            str += '</div></div> </a></div>';

          };
          var div = $('.liren');
          div.append(str)
          $(".liren_content:odd").css("float",'right');
    }
})

}

在H5页面的JS 怎么封装到另一个JS文件中呢

  • 写回答

2条回答

  • Go 旅城通票 2016-04-19 07:02
    关注

    创建一个js文件将代码贴进去保存,用script导入就行了,注意导入位置,要在jquery框架下面

    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀