2014-10-03 141 views
0

我有2个javascripts互相冲突,较新的(Zeroclipboard)与较旧的(删除行)冲突,并且不会让删除行一个工作。当我删除zeroclipboard one,删除工作。2 javascripts冲突

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

我有一个files.php文件,其中包括header.php文件。我在header.php中添加了custom.js文件,该文件包含许多用于整个项目操作的功能,包括删除行功能。而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

+0

哪里是'deleteRow()'使用的功能? 何处使用'$'和'init'以及'move_swf'?他们都需要在全球对象? – laruiss 2014-10-03 11:57:33

回答

0

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

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

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

希望这有助于

+0

大声笑,你的评论有帮助,我一直忘记按Ctrl + F5:\ 嗯,我不知道为什么该博客(代码)的原作者已经添加了。不知道js的基础知识,我不确定这行代码是不必要的。我假设一样,并继续删除该行代码,但它仍然没有帮助我。哦,等等,试验和错误我多次改变了我的custom.js。而一些较旧的缓存,我想所有我试过的解决方案可能已经工作。 :| – 2014-10-03 12:04:26

+0

我不知道为什么在博客中的例子是定义这个'function',因为它不是使用它。无论如何,这是很好的帮助你':))' – albciff 2014-10-03 12:13:52

+0

嘿,你可以帮助我添加一个警告或通知后点击共享按钮?我想提醒用户,文本一旦发生就被复制到剪贴板。 – 2014-10-03 12:26:06

0

这里是你应该如何使用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> 
+0

哦,很酷。感谢那。 – 2014-10-03 12:07:19