停止jQuery的重复,附加示例 [英] Stop jQuery from repeating, example attached

查看:82
本文介绍了停止jQuery的重复,附加示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将jQuery附加到发票上.有时我必须一次打印多张发票.发生这种情况时,每张发票都会显示我完全相同的jQuery,并且每次创建不需要的额外元素时都会运行它.有没有一种方法可以使jQuery出现更多,然后仅在它最后一次出现在代码中时才运行一次?感谢您的帮助.

I have to attach a jQuery to an invoice. Sometimes I have to print multiple invoices as once in a batch. When this happens my exact same jQuery appears for every invoice and it runs each time creating extra elements I do not need. Is there a way to have a jQuery that appears more then once only run once when it is the last time it appears in the code? Thanks for any help.

下面是示例.我以前曾发布过此消息,每个人都要求查看我的操作方式.我不确定如何将代码添加到帖子中,因为它说太长了.感谢你的帮助.你们真棒.

Example is below. I have previously posted this and everyone asked to see how I was doing it. I was not sure how to add the code to the post as it said it was too long. Thanks for all your help. You guys are awesome.

<table width=180 border=0 cellpadding=0 cellspacing=0>
                <tr> 
                  <td class="barcode_needed">10133</td>
                </tr>

<!--This section of code is attached to every order so it repeats when each order prints, (this is a simplified version of the code, I need to make it only run the last time it shows up-->                    
<link rel="stylesheet" type="text/css" href="http://barcode-coder.com/css/style.css?ft=1298939919" />
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-ui-1.7.custom.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-barcode-last.min.js"></script>


<script>
$('td.barcode_needed').append('<div class="bcTarget">');

$('.bcTarget').each(function() {
var $this = $(this);
$this.barcode(
    'G' + $this.closest('td.barcode_needed').text(),
    'code128'
);
});


</script>
<!--End Repeating Code-->
                <tr> 
                  <td class="barcode_needed">20133</td>
                </tr>


<link rel="stylesheet" type="text/css" href="http://barcode-coder.com/css/style.css?ft=1298939919" />
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-ui-1.7.custom.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-barcode-last.min.js"></script>


<script>
$('td.barcode_needed').append('<div class="bcTarget">');

$('.bcTarget').each(function() {
var $this = $(this);
$this.barcode(
    'G' + $this.closest('td.barcode_needed').text(),
    'code128'
);
});


</script>

                <tr> 
                  <td class="barcode_needed">30133</td>
                </tr>


<link rel="stylesheet" type="text/css" href="http://barcode-coder.com/css/style.css?ft=1298939919" />
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-ui-1.7.custom.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-barcode-last.min.js"></script>


<script>
$('td.barcode_needed').append('<div class="bcTarget">');

$('.bcTarget').each(function() {
var $this = $(this);
$this.barcode(
    'G' + $this.closest('td.barcode_needed').text(),
    'code128'
);
});


</script>

</table>

推荐答案

更改重复模板以包括支票:

Change your repeating template to include a check:

<script>
if (typeof alreadyDone === "undefined") {
   var alreadyDone = true;

   $(document).ready(function() {
     $('td.barcode_needed').append('<div class="bcTarget">');

     $('.bcTarget').each(function() {
       var $this = $(this);
       $this.barcode('G' + $this.closest('td.barcode_needed').text(),'code128');
     });    
   });
}
</script>

现在它应该只运行一次.

Now it should run only once.

另一种尝试,我们在删除类的同时...

Another thing to try, where we remove the classes as we go...

<script>
 $('td.barcode_needed').each(function() {
       $(this).append('<div class="bcTarget">');
 });

 $('.bcTarget').each(function() {
   $(this).barcode('G' + $(this).closest('td.barcode_needed').text(),'code128');
   $(this).removeClass('bcTarget');       
   $(this).closest('td.barcode_needed').removeClass('barcode_needed');
 });    
</script>

这篇关于停止jQuery的重复,附加示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆