2014-10-03 65 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