01/10/2018, 16:55

Giúp mình bị xung đột hàm trong jquery

mình có 2 hàm trong 1 trang

    	var sidebarcontainer = $("#sidebar-wrapper"),
	    sidebarbutton = $(".sidebar-button");
	snip = $('.index .post-snip,.index .post-cmt,#main-wrapper,.index .post-outer,.block-image,.index .post,.index .post h2');
	$(function () {
	    sidebarbutton.click(function () {
	        if (sidebarcontainer.is(":hidden")) {
	            sidebarcontainer.slideDown(200);
	            snip.removeAttr('style')
	        } else {
	            sidebarcontainer.fadeOut(200);
	            $('#main-wrapper').css({
	                'width': '100%'
	            });
	            $('.index .post-outer').css({
	                'width': '32%',
	                'float': 'left'
	            });
	            $('.block-image').css({
	                'width': '100%'
	            });
	            $('.index .post').css({
	                'padding': '15px 15px 0 15px',
	                'margin': '5px',
	                'margin-bottom': '25px'
	            });
	            $('.index .post h2').css({
	                'font-size': '17px',
	                'white-space': 'inherit',
	                'height': '55px'
	            });
	            $('.index .post-snip,.index .post-cmt').css({
	                'display': 'none'
	            })
	        }
	    })
	});

$(document).ready(function($){

  $('.dropdown').each(function() {
    var $dropdown = $(this);

    $(".dropdown-link", $dropdown).click(function(e) {
      e.preventDefault();
      $div = $(".dropdown-container", $dropdown);
      $div.toggle();
      $(".dropdown-container").not($div).hide();
      return false;
    });

});

$('html').click(function(){
    $(".dropdown-container").hide();
  });
     
});

function copyTextToClipboard(text) {
		var textArea = document.createElement("textarea");
		textArea.style.position = 'fixed';
		textArea.style.top = 0;
		textArea.style.left = 0;
		textArea.style.width = '2em';
		textArea.style.height = '2em';
		textArea.style.padding = 0;
		textArea.style.border = 'none';
		textArea.style.outline = 'none';
		textArea.style.boxShadow = 'none';
		textArea.style.background = 'transparent';
		textArea.value = text;
			document.body.appendChild(textArea);
		textArea.select();
		try {
    		var successful = document.execCommand('copy');
    		var msg = alert("Copy URL thành công!!");
    		
  		} catch (err) {
    		var msg = alert("Không thể sao chép liên kết!");
  		}
  		document.body.removeChild(textArea);
	}
	function CopyLink() {
		copyTextToClipboard(location.href);
	}

  $(function(){
 			new Clipboard('.copy-text');
		});

mình click 1 cái dưới mà nó có cả kết quả của cái trên. các bác giúp với

Tên Gì Cũng Được viết 19:04 ngày 01/10/2018

phải ghi rõ là đoạn code trên bạn mong muốn điều gì, và thực tế code chạy có gì khác so với ý muốn
“mình click 1 cái dưới mà nó có cả kết quả của cái trên. các bác giúp với” thì ai mà biết click cái dưới là click cái gì, hiện kết quả gì, kết quả của cái trên là gì (có kết quả dưới?)
Không có html thì làm sao mà biết trên document có gì để chạy, để click, để …

.dropdown-link trong $(".dropdown-link", $dropdown).click để làm gì khi nó ở trong vòng lặp?

$('html').click(functio    n(){
    $(".dropdown-container").hide();
});
// câu này có tác dụng là click vô bất kì đâu trên cái web 
// cũng ẩn hết tất cả element có class ".dropdown-container" ???

bản chất của $.click là thêm event listener cho element mà thôi, chứ bản thân jquery nó không listen event đâu, nên có thể code bị add 2 lần

Bài liên quan
0