2016-08-05 108 views
-1

如何解决2 JavaScript之间的冲突,但其中一个作品,但不是第二个,但当我分开他们,他们工作很好。只有当他们在同一个页面时,冲突才会发生,而我首先提出的那个始终不是我先放哪一个。我到处都找过,但我似乎无法在这里找到一个答案是我的脚本也无所谓,如果我删除之间的冲突2 javascript

<body onload="dothis();dothat()"> 

function dothis(){ 
 
var z = 1; //value to make div overlappable 
 

 
$('#addText').click(function (e) { 
 
    /** Make div draggable **/ 
 
    $('<div />', { 
 
     class: 'ui-widget-content', 
 
     appendTo: '.container4', 
 
     draggable: { 
 
      containment: 'parent', 
 
      start: function(event, ui) { 
 
       $(this).css('z-index', ++z); 
 
      } 
 
     } 
 
    }); 
 
}); 
 

 

 
$(document).on("dblclick", '.text1', function() 
 
{ 
 
    $(this).hide(); $(this).closest('.item1').find('.edit_text1').val($(this).text()).show(); 
 
}); 
 

 
$(document).on("click", ".edit_text1", function() 
 
{ 
 
    return false; 
 
}); 
 

 

 
$(document).on("click", function() 
 
{ 
 
    var editingText = $('.edit_text1:visible'); 
 
    if (editingText.length) 
 
    { 
 
     editingText.hide(); 
 
     editingText.closest('.item1').find('.text1').text($(editingText).val()).show(); 
 
    } 
 
}); 
 

 

 
    var count = 1; 
 
var selectedDraggable; 
 

 
ko.bindingHandlers.draggable={ 
 
    init: function(element, valueAccessor, allBindingsAccessor, viewModel) { 
 
     $(element).draggable(); 
 
     $(element).addClass('item1' + count); 
 
     count++; 
 
     $(element).on('click', function() { 
 
      selectedDraggable = $(this); 
 
     }) 
 
    } 
 
}; 
 

 

 
var vm=function(){ 
 
    var self=this; 
 
    self.items1=ko.observableArray(); 
 
    self.textContent1 = ko.observable(''); 
 
    self.init=function(){ 
 
     self.items1([]); 
 
    } 
 
    self.remove=function(item){ 
 
     console.log(item); 
 
     self.items1.remove(item); 
 
    } 
 
    self.addNew1 = function() { 
 
     self.items1.push(self.textContent1()); 
 
     self.textContent1(''); 
 
    } 
 
    self.init(); 
 
} 
 

 
ko.applyBindings(new vm()); 
 

 

 

 
$("#fss").change(function() { 
 
    selectedDraggable.css("font-family", $(this).val()); 
 
}); 
 

 

 

 
$("#size1").change(function() { 
 
    selectedDraggable.css("font-size", $(this).val() + "px"); 
 
}); 
 

 

 
$('.fooo').click(function(){ 
 
    selectedDraggable.css("color", $(this).attr('data-color')); 
 
}); 
 

 
$(document).ready(function(){ 
 
    $("#rotateButton1").click(function(){ 
 
     var x= $("#rotateInput").val();  
 
    selectedDraggable.css('transform','rotate(' + x + 'deg)'); 
 
     
 
    }); 
 
}); 
 
}​ 
 

 

 

 

 

 
function dothat(){ 
 
var z = 1; //value to make div overlappable 
 

 
$('#addText').click(function (e) { 
 
    /** Make div draggable **/ 
 
    $('<div />', { 
 
     class: 'ui-widget-content', 
 
     appendTo: '.container', 
 
     draggable: { 
 
      containment: 'parent', 
 
      start: function(event, ui) { 
 
       $(this).css('z-index', ++z); 
 
      } 
 
     } 
 
    }); 
 
}); 
 

 

 
$(document).on("dblclick", '.text', function() 
 
{ 
 
    $(this).hide(); $(this).closest('.item').find('.edit_text').val($(this).text()).show(); 
 
}); 
 

 
$(document).on("click", ".edit_text", function() 
 
{ 
 
    return false; 
 
}); 
 

 

 
$(document).on("click", function() 
 
{ 
 
    var editingText = $('.edit_text:visible'); 
 
    if (editingText.length) 
 
    { 
 
     editingText.hide(); 
 
     editingText.closest('.item').find('.text').text($(editingText).val()).show(); 
 
    } 
 
}); 
 

 

 
    var count = 1; 
 
var selectedDraggable; 
 

 
ko.bindingHandlers.draggable={ 
 
    init: function(element, valueAccessor, allBindingsAccessor, viewModel) { 
 
     $(element).draggable(); 
 
     $(element).addClass('item' + count); 
 
     count++; 
 
     $(element).on('click', function() { 
 
      selectedDraggable = $(this); 
 
     }) 
 
    } 
 
}; 
 

 

 
var vm=function(){ 
 
    var self=this; 
 
    self.items=ko.observableArray(); 
 
    self.textContent = ko.observable(''); 
 
    self.init=function(){ 
 
     self.items([]); 
 
    } 
 
    self.remove=function(item){ 
 
     console.log(item); 
 
     self.items.remove(item); 
 
    } 
 
    self.addNew = function() { 
 
     self.items.push(self.textContent()); 
 
     self.textContent(''); 
 
    } 
 
    self.init(); 
 
} 
 

 
ko.applyBindings(new vm()); 
 

 

 

 
$("#fs").change(function() { 
 
    selectedDraggable.css("font-family", $(this).val()); 
 
}); 
 

 

 

 
$("#size").change(function() { 
 
    selectedDraggable.css("font-size", $(this).val() + "px"); 
 
}); 
 

 

 
$('.foo').click(function(){ 
 
    selectedDraggable.css("color", $(this).attr('data-color')); 
 
}); 
 

 
$(document).ready(function(){ 
 
    $("#rotateButton").click(function(){ 
 
     var x= $("#rotateInput").val();  
 
    selectedDraggable.css('transform','rotate(' + x + 'deg)'); 
 
     
 
    }); 
 
}); 
 
}​
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script> 
 
<script 
 
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script> 
 
<link rel="stylesheet" 
 
href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" /> 
 
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> 
 
    <link rel="stylesheet" 
 
href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
 
<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
 

 
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
 

 
<script src="http://circletype.labwire.ca/js/circletype.js"></script><script src="http://tympanus.net/Development/Arctext/js/jquery.arctext.js"></script> 
 
<link rel="stylesheet" href="/resources/demos/style.css"> 
 

 

 

 

 

 
<body onload="dothis();dothat()">​

+0

在onclick中返回false将停止事件传播,请尝试返回true –

+2

关于在SO上提出问题的几个提示 - 首先,我建议将代码格式化得更好一些,以使其更具可读性。其次,你说有冲突,但你没有说停止工作 - 不要指望我们为你做基本的调试,试着描述什么停止了一开始工作,没有人想要拖拽200行糟糕的格式化的代码,只是找出你在说什么 –

+0

@YanjunLin返回true没有工作 – user6483684

回答

-2
/* 
⚐ A Revealing Module Pattern (Public & Private) w Public Namespace 'myscript' like below avoids collisions with other scripts. 

    myScript.color --> red 
    myScript.hello('Jane') --> Hello Jane 
*/ 

var myScript = (function() { 

    // reference document only once for performance 
    var doc = document; 

    // object to expose as public properties and methods such as clock.now 
    var pub = {}; 

    //myscript.color 
    pub.color = 'red'; 

    //myscript.hello 
    pub.hello = function (name) { 
     alert('Hello ' + name); 
    }; 

    //API 
    return pub; 
}());