2个java脚本冲突

2 javascripts are conflicting

本文关键字:冲突 脚本 java 2个      更新时间:2023-09-26

我有两个相互冲突的java脚本,新的(Zeroclipboard)与旧的(删除行)冲突,并且不会让删除行一个工作。我删除零剪贴板的那一刻,删除就成功了。

尝试添加jQuery.noConflict();但似乎不起作用。通过阅读一些解决方案,我决定删除$符号,但仍然没有。

我有一个files.php文件,包括header.php文件。我正在header.php中添加custom.js文件,该文件包含许多用于整个项目操作的函数,包括delete行函数。然而,ZerClipboard的新脚本在files.php中。

旧的一个,点击删除图标删除一个表行,在我添加下一个后就不起作用了:

custom.js

function deleteRow()
  {
    var current = window.event.srcElement;
    while ( (current = current.parentElement)  && current.tagName !="TR");
         current.parentElement.removeChild(current);
  }
$(document).ready(function()
    {
        $('table#delTable td a.delete').click(function()
        {
            if (confirm("Are you sure you want to delete?"))
            {
                var fid = $(this).parent().parent().attr('fid');
                var str=$(this).attr('rel');
                var data = 'fid=' + $(this).attr('rel') + '&uid=' + $(this).parent().attr('rel');   
                var deletethis = '#tr' + $(this).attr('rel');           
                var parent = $(this).parent().parent();
                $.ajax(
                {
                       type: "POST",
                       url: "delete.php",
                       data: data,
                       cache: false,
                       success: function(msg)
                       {
                            $(deletethis).fadeOut('slow', function() {$(this).remove();});
                       }
                });             
        }
    });
    $('table#delTable tr:odd').css('background',' #FFFFFF');
});

ZeroClipboard的JS和SWF,连同此JS在剪贴板上复制一些文本的共享图标点击:

files.php

<script type="text/javascript" src="js/ZeroClipboard.js"></script>
<script language="JavaScript">
    var clip = null; 
    function $(id) { return document.getElementById(id); } 
   function init() 
   {
      clip = new ZeroClipboard.Client();
      clip.setHandCursor( true );
   } 
   function move_swf(ee)
   {    
      copything = document.getElementById(ee.id+"_text").value;
      clip.setText(copything); 
         if (clip.div)
         {    
            clip.receiveEvent('mouseout', null);
            clip.reposition(ee.id);         }
         else{ clip.glue(ee.id);   }  
         clip.receiveEvent('mouseover', null);  
   }    
</script>

我用这篇博客文章实现了多个zerclipboard-http://blog.aajit.com/easy-multiple-copy-to-clipboard-by-zeroclipboard/下面是files.php页面生成的HTML源代码-http://jpst.it/tlGU

删除第二个脚本的以下函数定义:

function $(id) { return document.getElementById(id); }

因为这是在window上下文中重新定义$对象,所以当您在第一个脚本中使用$时,您不是在使用jquery,而是在使用新的函数定义。

希望这有帮助,

以下是应该如何使用noConflict():

function deleteRow()
  {
    var current = window.event.srcElement;
    while ( (current = current.parentElement)  && current.tagName !="TR");
         current.parentElement.removeChild(current);
  }
jQuery.noConflict(); // Reinitiating $ to its previous state
jQuery(document).ready(function($) // "Protected" jQuery code : $ is referencing jQuery inside this function, but not necessarily outside
    {
        $('table#delTable td a.delete').click(function()
        {
            if (confirm("Are you sure you want to delete?"))
            {
                var fid = $(this).parent().parent().attr('fid');
                var str=$(this).attr('rel');
                var data = 'fid=' + $(this).attr('rel') + '&uid=' + $(this).parent().attr('rel');   
                var deletethis = '#tr' + $(this).attr('rel');           
                var parent = $(this).parent().parent();
                $.ajax(
                {
                       type: "POST",
                       url: "delete.php",
                       data: data,
                       cache: false,
                       success: function(msg)
                       {
                            $(deletethis).fadeOut('slow', function() {$(this).remove();});
                       }
                });             
        }
    });
    $('table#delTable tr:odd').css('background',' #FFFFFF');
});

在files.php中:

<script src="js/ZeroClipboard.js"></script>
<script>
   var clip = null;
   function $(id) {
       return document.getElementById(id);
   }
   function init() {
       clip = new ZeroClipboard.Client();
       clip.setHandCursor(true);
   }
   function move_swf(ee) {
       copything = document.getElementById(ee.id + "_text").value;
       clip.setText(copything);
       if (clip.div) {
           clip.receiveEvent('mouseout', null);
           clip.reposition(ee.id);
       } else {
           clip.glue(ee.id);
       }
       clip.receiveEvent('mouseover', null);
   }
</script>