2017-09-16 7 views
0

materialize를 사용하여 내 페이지에 아코디언 축소 가능을 만듭니다. 접을 수있는 머리글을 클릭 할 때 gif/이미지를 내 페이지에 추가하고 접을 수있는 머리글을 다시 클릭하거나 다른 머리글을 클릭 할 때이를 제거하려고합니다.클릭하면 gif를 추가하고 제거하는 방법은 무엇입니까?

클릭 할 때 GIF를 추가 할 수는 있지만 삭제 방법을 알 수는 없습니다. 나는 자바 스크립트와 jquery를 사용하고있다.

contentTitles = ["Test1", "Test2", "Test3"]; 
contentLinks = ["test1.gif", "test2.gif", "test3.gif"]; 

$(document).ready(function(){ 
    $('.collapsible').collapsible(); 
    $('main').append('<ul data-collapsible="accordion"></ul>'); 
    $('ul').addClass('collapsible popout'); 

for (var j = 0; j < contentTitles.length; j++) { 
    $('ul').append('<li></li>'); 
    // start the creation of the collapsible 
    $('.collapsible').collapsible({ 
     accordion: false, // A setting that changes the collapsible behavior to expandable instead of the default accordion style 
    }); 
    }; 

// create the collapsible 
$('li').append('<div><i></i></div>'); 
$('li').append('<div><span></span></div>'); 
$('i').addClass('material-icons'); 
$('i').parent().addClass("collapsible-header hoverable"); 
$('span').parent().addClass("collapsible-body"); 

// adds the titles to each collapsible header 
$('.collapsible-header').each(function(index) { 
    $(this).html(contentTitles[index]); 
}) 

// adds the gif urls to each collapsible body 
$('.collapsible-header').on('click', function() { 
    $('.collapsible-body').each(function(index) { 
    $(this).html('<iframe class="gif" src=' + contentLinks[index] + '>'); 
    }) 
}) 

$('.collapsible-header').on('click', function() { 
    $('.collapsible-body').html(); 
}) 

}) 
}); 

감사합니다 :

다음은 내 코드입니다.

답변

0

.html() 호출은 요소의 html 콘텐츠를 반환하기 만하면 변경되지 않습니다. 당신이 요소를 비워하려면 다음을 수행하십시오 나는 그들 중 하나를 볼 수 있어요 전에

$('.collapsible-header').on('click', function() { 
    $('.collapsible-body').html(""); //set HTML to be an empty string 
}) 
+0

감사 응답 을 위해 내가 빈 문자열에 HTML을 설정하면 GIF가 제거됩니다. – samiam0906

+0

두 개의 클릭 핸들러가 연결되어 있다고 생각합니다. 그 중 하나는 gif를 추가하고 다른 하나는 즉시 제거합니다. 하나의 핸들러 만 사용하고 제거 할 GIF가 있는지 확인한 다음 필요에 따라 추가하거나 제거 할 수 있습니다. –